0

First of all I would like to tell you that I am kinda new in the topic of developing wordpress. On the other hand I find it very powerful that we can 'code' our wordpress site!

My issue is the following: I would like to change(replace) my main navigation menu by post or category. Because if i click on a post of a category, my navigation menu will go to the standard one which i do not want. I tried to look for some solution over the internet and here but I could not find any, unfortunately. I came up with the following idea then: What if I change my menu args in the functions.php? My code snippet what I wrote in the functions.php is the following:

add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );

function bb_wp_nav_menu_args( $args = '' ) {
    if( is_single('123') ) { 
        $args['menu_id'] = '12'; 
    }
    if( is_single('116') ) { 
        $args['menu_id'] = '12';
    } 
    if( is_category('15') ) { 
        $args['menu_id'] = '12'; 
    } 
    return $args;
}

Here the 123 and 116 are the ID's of posts. And naturally the number 15 is the category...but unfortunately it does not work. What is your opinion? Where does the problem lie? Would you be so kind as to help me in this matter? Thanks in advance and sorry if my question is very newby!

Edit: I would like to add some additional information because these might be relevant: I am using Hueman theme and I would like to change the header menu instead of the topbar menu. Thanks again!

1 Answer 1

0

This code will check to ensure that the theme_location is header. Also, menu is the parameter where the desired WordPress Nav menu is specified. menu_id is the HTML ID that is applied to the <ul> element which forms the menu. Docs on the arguments for wp_nav_menu() can be found here.

add_filter( 'wp_nav_menu_args', 'bb_wp_nav_menu_args' );
function bb_wp_nav_menu_args( $args ) {

    if ( 'header' !== $args['theme_location'] ) {
        return $args;
    }

    if ( is_single( '123' ) ) {
        $args['menu'] = '12'; // Replace with desired menu ID, slug, or name 
    } else if ( is_single( '116' ) ) {
        $args['menu'] = '12'; // Replace with desired menu ID, slug, or name  
    } else if ( is_category( '15' ) ) {
        $args['menu'] = '12'; // Replace with desired menu ID, slug, or name  
    }

    return $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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