Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have one category and under this category I have many posts. Normally when I go to my category page from menu it added active class class="current-menu-item". Bellow is the rendered html

<ul id="menu-mainmenu" class="menu">
<li><a href="http://localhost/site/">Home</a></li>
<li><a href="http://localhost/site/?page_id=7">Rentals</a></li>
<li class="current-menu-item"><a href="http://localhost/site/?cat=1">Products</a></li>
<li><a href="http://localhost/site/?page_id=11">Services</a></li>
<li><a href="http://localhost/site/?page_id=9">company</a></li>
<li><a href="http://localhost/site/?page_id=13">Contact US</a></li>
</ul>

But from the category page when I go to any post, there is no active class added. It is normal I think. But I want to keep my active class when I go to that category posts . How can I do that?

Bellow is my active class code in function

add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);

function my_css_attributes_filter($var) {
  return is_array($var) ? array_intersect($var, array('current-menu-item', 'current-menu-ancestor')) : '';

}
share|improve this question
    
You want set the category active class in the single post page ? –  Klian Apr 28 at 6:40
    
yes.. when i will visit category single post that time also class="current-menu-item" will be one product menu –  Accore LTD Apr 28 at 8:04
    
Is there a class 'current-page-anchestor' or something like that?, otherwise you can hardcode it in the header using a conditional statement for only that category. –  Maartje Apr 28 at 9:52
    
current-page-anchestor is fine if i can do but i don't know how. hardcode also ok. but i want to how. i got some code in web but actually none is work. –  Accore LTD Apr 29 at 4:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.