I want to add current-page-anchestor when a single post is browsed. I was looking for that function code and tried many, but with not success yet. Now I am thinking I can do it that way, then it would work. Like

if ... the post from this category ... then add current-page-anchestor etc, something like that. So then, when I visit my single post, my menu will be highlight too .

You can also see details what I am looking for here

My current nav code is below

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')) : '';

  • Do you use WP native nav? Why don't you use default WP classes? I'm pretty sure WP ads such class to category menu item itself. What WP version do you use? – Krzysiek Dróżdż May 1 '14 at 8:48
  • which default class you mean. can you please tell me details please. i am using all latest WP 3.9 – Accore LTD May 2 '14 at 4:13
  • OK. So if single post is displayed and it's in A category, then A category item will have these classes: "menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-XXX". You can use "current-post-ancestor current-menu-parent current-post-parent" to do what you want. But maybe I've misunderstood something. – Krzysiek Dróżdż May 2 '14 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.