0

I have a problem with menu organization. I have a Pages with url "pressroom" and have a Category with the same name. I want to make it a one menu item.

Pressroom (sitename.com/pressroom/)

  • page 1 (sitename.com/pressroom/page1/)
  • page 2 (sitename.com/pressroom/page2/)
  • category with posts (sitename.com/category/pressroom/news/)

When I am in Category - nav_menu parent item have a correct css selector current-menu-ancestor current-menu-parent, but when I am go to post of this category (url changed to sitename.com/pressroom/news/postid/) - css selector of parent menu item disappear.

How can I correct this?

  • I don't understand. You want the menu items to treat categories like "parents" of the posts? Categories are not "parents" of posts, why would they have a selector identifying them as such? – s_ha_dum Feb 15 '13 at 14:33
2

Check nav menu target URL for '/pressroom/', then check if it's a post. If it fits, add custom class to that menu item.

<?php
function my_add_posts_page_ancestor_class( $classes, $item ) {
    if( false !== strpos($item->url, '/pressroom/')
    && is_single($item->ID)
    && !is_page($item->ID) ) {
        $classes[] = 'my-ancestor-class';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'my_add_posts_page_ancestor_class', 10, 2);

The code is untested and can be improved. It's a direction to think about.

Update

See what I've found: Add a class to wp_nav_menu() items with URLs included in the current URL

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.