1

I'm using the following code to force my wordpress navigation to reflect the category of the current post.

It works perfectly, but I need help to extend this function to more than just 1 category and menu item. Tried duplicating the entire code but with different category and menu item but wordpress throws an "already declared" error as expected.

Many thanks for any help or advice..

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

function add_custom_class( $classes = array(), $menu_item = false ) {
    if ( is_single() && in_category( 'Services' ) && 1633 == $menu_item->ID && ! in_array( 'current-menu-item', $classes ) ) {
        $classes[] = 'current-menu-item';
    }
    return $classes;
}
3

You declared the same function name, that's the cause of the "declared error". Just change the class function (including in the filter hook). Or you can just extend your function code. I also would rewrite the if structure to have a more easy readibility:

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

function add_custom_class( $classes = array(), $menu_item = false ) {

    //Check if already have the class
    if (! in_array( 'current-menu-item', $classes ) ) {

        //Check if it's the ID we're looking for
        if ( 1633 == $menu_item->ID ) {

            //Check if is in a single post
            if ( is_single() ) {

                //Check if the single post is in the category
                if ( in_category( 'Services' ) ) {

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

                }

            }

        } elseif ( 1634 == $menu_item->ID ) {

            //Check if is in a single post
            if ( is_single() ) {

                //Check if the single post is in the category
                if ( in_category( 'Products' ) ) {

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

                }

            }

        }

    }

    return $classes;
}

Besides that, this code is almost non-dynamic, since you must specify the menu ID and the category name. If I understood your menu logic, you're trying to add the 'current-menu-item' to a category menu, when a single post have that category. I would code something similar to: (so it would work with any category and post combination):

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

function add_custom_class( $classes = array(), $menu_item = false ) {

    //Check if already have the class
    if (! in_array( 'current-menu-item', $classes ) ) {

        //Check if it's a category
        if ( 'category' == $menu_item->object ) {

            //Check if the post is in the category
            if ( in_category( $menu_item->ID ) ) {

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

            }

        }

    }

    return $classes;
}
  • Thanks so much Bruno - the first answer was exactly what I needed :) – speedypancake Nov 19 '15 at 14:19

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.