0

I know that this question was asked many times, but I did not find a really good solution for my problem:

I have a custom post type "event" with some categories like "lecture". For each category, I created an individual page which shows category infos and a list of the events from this category. The pages are all accessible from submenu entrys.

If I open an event, no menu item is highlighted as current. I think the best solution would be to "link" each category to it's page, but I don'T know how to do this.

Adding the proper classes to the menu items and and their parents would be good, but modifying the nav menu walker would be perfect (another addon nneeds to know which one is the current menu entry to show the current submenu in a second navbar).


My current solution is the following filter:

function my_special_nav_class( $classes, $item ) {
    global $post;

if (get_post_type() == "event") {
    $page_categories = wp_get_post_terms($post->ID, 'event-categories');
    if ($page_categories) {
        foreach( $page_categories as $category ) {
            $ignored_terms = array('CRM', 'PPM');
            $cat_name = $category->name;
            if ( !in_array($cat_name, $ignored_terms) ) {
                if ($cat_name == $item->title) {
                    $classes[] = 'current-menu-item';
                }
            }
        }
    }
}   
return $classes; 
} 
add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );

But how can I add a class to the parent menu items of the current one?

  • Hi jannnik, could you provide more details on what you've tried already (and why it didn't work)? – kraftner Jan 30 '15 at 13:17
  • you should inspect the element on your browser. There are classe in there. Maybe you're not making it in css... – Reigel Jan 30 '15 at 13:20
  • Sorry, but I reverted all changes. But I think it would not help anyway. For example I tried to understand the code of this plugin and extend it to my category-problem, but I think it was too complex for my actual wordpress coding knowledge. wordpress.org/plugins/current-menu-item-for-custom-post-types -> I want the same thing this plugin does, but it has to be extended for the custom post type's categories. – jannnik Jan 30 '15 at 13:23
-1

I think i have a function that will do it for you.

The usual WP way to highlight the menu item is to add the class "current-menu-item" to the item, then you can style highlighted links with CSS.

In this case you want to highlight a taxonomy menu link if we're in a post for that taxonomy, is that right? For example if we're in a post in "CategoryX" we want the menu link for "CategoryX" to be highlighted.

The following filter for Walker_Nav_Menu should help. This can go in functions.php

function my_special_nav_class( $classes, $item ) {

if( 'category' == $item->object ){  

    $current_category = array();   

    $category = get_category( $item->object_id );  
    $category = $category->term_id;

    global $wp_query, $wp_rewrite;

    $queried_object = $wp_query->get_queried_object();
    $current_category[] = $queried_object->term_id;

    // uncomment the following if you want to debug:
    // echo '<pre>Category of this menu item<br />';
    // print_r($category);
    // echo '</pre>';
    // echo '<pre>Category queried for this page<br />';
    // print_r($current_category);
    // echo '</pre>';

    if( in_array( $category, $current_category ) ) {

        $classes[] = 'current-menu-item';   

    }
}   
    return $classes; 
} 


add_filter( 'nav_menu_css_class', 'my_special_nav_class', 10, 2 );

This function filters nav_menu_css_class which is used by Walker_Nav_Menu.

https://core.trac.wordpress.org/browser/tags/4.1/src/wp-includes/nav-menu-template.php#L427

The filter above works for the default taxonomy "category", but we would probably have to loop through all current post taxonomies to catch a custom taxonomy. If you only need one taxonomy, it's simpler. I'm still working out a solution for custom taxonomies...

  • Thanks for your answer. The code did not work because I'm using a custom taxonomy. I added my current "solution" to the question above, but my problem is not solved completely: How to add a "current"-class to the parent menu items? – jannnik Feb 24 '15 at 11:28

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.