根据当前页面突出显示菜单项

问题描述:

我试图突出显示菜单项,具体取决于它是否位于当前页面上。下面的代码当前突出显示每个菜单项,因为它位于foreach循环内。我如何突出显示分类术语,如果它是在某个页面上,并且只有该术语(而不是每个术语)?根据当前页面突出显示菜单项

    <?php $args = array('taxonomy' => 'blog'); 
         $terms = get_terms('blog', $args); 

         $count = count($terms); $i=0; 
         if ($count > 0) { 
          $cape_list = '<p class="my_term-archive">'; 
          echo $postid; 
          foreach ($terms as $term) { 
           $i++; 
           $absolute = get_bloginfo('url'); 
           $postid = get_the_ID(); 
           if($postid == "561") { 
            $term_list .= '<a class="menu-active" style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>'; 
           } else { 
            $term_list .= '<a style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>'; 
           } } ?> 

          <?php echo $term_list; } ?> 
+1

WordPress的有一个内置的导航功能。你为什么不使用它? http://codex.wordpress.org/Function_Reference/wp_nav_menu它添加一个类,如果它是选定的页面。 – CharliePrynn

+0

@CharliePrynn我很乐意使用,但任何想法如何将我的代码工作?它列出了自定义分类术语,因此它不是一个简单的页面列表。 – Rob

您将不得不查询属于当前页面的分类术语,并在foreach循环中比较它们的id -s。

get_the_ID()函数返回当前帖子的id,而不是当前分类。

这里是一个WordPress相关问题一个链接,显示了如何查询当前的分类术语:

https://wordpress.stackexchange.com/questions/20431/how-to-get-taxonomy-term-of-the-current-page-and-populate-queries-in-the-templat