自定义帖子类型活动链接 - WordPress的

问题描述:

我有一个自定义帖子类型名为'学校'在我的WordPress的网站。当您将鼠标悬停在学校标签上时,子菜单中会显示学校列表。这些是我用不同的学校名称创建的页面。现在,当您点击某个学校页面时,我会为所有学校提供一个侧栏,以便他们可以浏览侧栏中的不同学校,而不是使用菜单。自定义帖子类型活动链接 - WordPress的

我有使用下面的代码片段填充边栏。

while($shools_loop->have_posts()) : $schools_loop->the_post(); 

    $content .= '<li class="schools-list">'; 
    $content .= '<a href="'.get_permalink().'">'.get_the_title().'</a>'; 
    $content .= '</li>'; 

endwhile; 

这工作完美,我可以浏览所有学校没有问题从我的侧边栏。我试图找到一种方法,当我通过侧边栏或导航来查看学校时,当我在活动页面上时,我为ACTIVE页面的li创建了一些css样式。我已经想出了如何使用导航菜单来做到这一点。但需要在侧边栏菜单上的帮助。由于侧边栏列表菜单正在填充,因此我不确定如何检查自定义帖子类型链接是否处于活动状态并对应于/ schools/get-title页面。

,我发现这样的事情在网上和我试着编辑它,但我不知道这是否仅适用于导航菜单

add_action('init', 'create_post_type'); 
function create_post_type() { 
    register_post_type('services', 
    array(
     'labels' => array(
     'name' => __('Services'), 
     'singular_name' => __('Services') 
    ), 
    'public' => true, 
    'has_archive' => true, 
    'rewrite' => array('slug' => 'services'), 
    ) 
); 
} 

// highlight active custom post page in nav 
add_filter('nav_menu_css_class', 'namespace_menu_classes', 10, 2); 
function namespace_menu_classes($classes , $item){ 
    if (get_post_type() == 'services') { 
    // remove unwanted classes if found 
    $classes = str_replace('current_page_parent', '', $classes); 
    // find the url you want and add the class you want 
    if ($item->url == 'services/physical-therapy-services/') { 
     $classes = str_replace('menu-item', 'menu-item current_page_parent', $classes); 
    } 
    } 
    return $classes; 
    } 

基本上需要找到一种方法来检查是否有自定义后类型是否有效。

+0

的 “服务” 应改为 “学校” –

您可以设置在主模板页global变量与当前帖子的ID,然后在你的侧边栏圈,你可以检索global变量,并通过使用get_the_ID()功能,它与目前的职位的ID进行比较,然后再执行必要的行动。

例子:

单cpt.php

// Inside the loop 
global $post_id = get_the_ID(); 

侧边栏cpt.php

$post_id = isset($_GLOBALS['post_id']) ? $_GLOBALS['post_id'] : 0; 
while($shools_loop->have_posts()) : $schools_loop->the_post(); 
    if($post_id == get_the_ID()){ 
     // This is the active link 
     $content .= '<li class="schools-list selected">'; 

    } else { 
     $content .= '<li class="schools-list">'; 

    } 

    $content .= '<a href="'.get_permalink().'">'.get_the_title().'</a>'; 
    $content .= '</li>'; 

endwhile; 
+0

可以显示我举一个粗略的例子来说明如何做到这一点? –