1
votes

I've created a wordpress menu with wp_nav_menu.

my structure looks like:

  • Page
    • Category
  • Page
  • Page
  • Page

If I open a post in "Category", there is no "current" class on the top level of the menu – only on "Category". Nesting only with pages works fine on multi level menus.

Is there a way to fix that?

2

2 Answers

2
votes

1) Either write a custom walker http://codex.wordpress.org/Function_Reference/wp_nav_menu#Using_a_Custom_Walker_Function to add a current class when that category is called;

2) Or, (not the most elegant): find your page ID for the menu item you want - 1000 in the example below - to highlight as current, select that category - is_category - and then add the current class - addClass - with jQuery:

<?php if (is_category('my-category')) { ?>

    <script type="text/javascript">

        jQuery(function($) {
        $(document).ready(function() {
        $('#menu-main-menu li.menu-item-1000').addClass('current-menu-item');
        }); });

        </script>

    <?php } ?>
1
votes

ahh... found the solution in the wordpress codex. this works fine for me:

add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' );
function add_menu_parent_class( $items ) {

    $parents = array();
    foreach ( $items as $item ) {
        if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
            $parents[] = $item->menu_item_parent;
        }
    }

    foreach ( $items as $item ) {
        if ( in_array( $item->ID, $parents ) ) {
            $item->classes[] = 'menu-parent-item'; 
        }
    }

    return $items;    
}