1

In WordPress, I want to display a different menu on the front page than on all other pages (without plugin). I'm using a theme I created myself.

This is the code:

functions.php

register_nav_menus(
    array(
        'menu-1' => esc_html__( 'Main navigation', 'theme' ),
        'menu-2' => esc_html__( 'Sub Menu', 'theme' ),
        'menu-3' => esc_html__( 'Footer - Navigation', 'theme' ),
    )
);

function theme_wp_nav_menu_args( $args = '' ) {

    if( is_front_page() ) { 
        $args['menu-1'] = 'Main navigation - front page';
    } else { 
        $args['menu-1'] = 'Main navigation - sub page';
    } 
        return $args;
    }
    add_filter( 'wp_nav_menu_args', 'theme_wp_nav_menu_args' );

header.php (dynamically loaded into all pages)

    <?php
        wp_nav_menu(
            array(
                'theme_location' => 'menu-1',
                'menu_id'        => 'primary-menu',
            )
        );
    ?>

The strange thing is, this code worked on localhost. After uploading it to the server and a series of changes in the WordPress dashboard, it stopped working.

I tried to recreate the process, but without success. So now I'm wondering, is some thing incorrect in my code? What am I missing?

EDIT:

I tried different suggestions (thank you all, so far!).

If I put this in my HTML, WordPress will always use the footer navigation as main navigation, even though in the dashboard, footer is clearly set as such - and only such:

        if( is_front_page() ) { 
            wp_nav_menu(array('menu'=>'menu-1','menu_id'=>'primary-menu'));
            } else { 
            wp_nav_menu(array('menu'=>'menu-2','menu_id'=>'primary-menu'));
        } 

I find that weird, because in functions.php, footer is menu-3. But this code doesn't mention menu-3 anywhere.

By using this in my HTML, the main navigation shows as main navigation and footer only as footer:

        wp_nav_menu(
            array(
                'theme_location' => 'menu-1',
                'menu_id'        => 'primary-menu',
            )
        );

I tried vicky's suggestion for my functions.php, but I keep having the same issue that the same main menu is used for front page and other pages...

2
  • all dashboard changes are you made it in server correctly? like set the home page in settings page?
    – vicky
    Jul 26, 2020 at 6:31
  • In page settings, home page and blog page are set. I set them again and saved it to make sure, but no changes. In the menu settings, ` 'Main navigation - sub page' ` is set as standard, same as on localhost. If I set ` 'Main navigation - front page'` as standard, it applies it to the sub pages as well. On localhost, it used 'Main navigation - front page' for the front page, due to my if-statement (I hope).
    – Cinder
    Jul 26, 2020 at 6:43

3 Answers 3

1

Remove the filter hook wp_nav_menu_args from your functions.php and put the below code in your header.php

if( is_front_page() ) { 
   wp_nav_menu(array('menu'=>'menu-1','menu_id'=>'primary-menu'));
} else { 
   wp_nav_menu(array('menu'=>'menu-2','menu_id'=>'primary-menu'));
} 
1
  • 1
    I did that. In the WP dashboard page settings, I set Main navigation as menu for the front page and Sub Menu for all other pages. But now for some reason it will always use the footer navigation as main navigation, even though the footer navigation is clearly set as such. menu-3 only appears in the code for my footer and in the dashboard, it is defined as footer navigation as well.
    – Cinder
    Jul 26, 2020 at 7:01
1

Try this

register_nav_menus(
    array(
        'menu-1' => esc_html__( 'Main navigation', 'theme' ),
        'menu-2' => esc_html__( 'Sub Menu', 'theme' ),
        'menu-3' => esc_html__( 'Footer - Navigation', 'theme' ),
    )
);

function theme_wp_nav_menu_args( $args = '' ) {

    if( is_front_page() ) { 
        $args['menu_id'] = 'Main navigation - front page';
    } else { 
        $args['menu_id'] = 'Main navigation - sub page';
    } 
        return $args;
    }
    add_filter( 'wp_nav_menu_args', 'theme_wp_nav_menu_args' );
1

Based on the suggestions above, I've edited my code. It's now working! :) Thanks everyone!

(note that names are slightly different in this version since I changed those as well once it was working)

The working code in functions.php

register_nav_menus(
    array(
        'menu-1' => esc_html__( 'Navigation 1', 'theme' ),
        'menu-2' => esc_html__( 'Navigation 2', 'theme' ),
        'menu-3' => esc_html__( 'Fußleiste - Navigation', 'theme' ),
    )
);

function praxis_wp_nav_menu_args( $args = '' ) {
 
    if( is_front_page() ) { 
        $args['menu-1'] = 'Startseite';
    } else { 
        $args['menu-2'] = 'Unterseiten';
    } 
        return $args;
    }

And in header.php:

        if( is_front_page() ) { 
            wp_nav_menu(array('theme_location'=>'menu-1','menu_id'=>'primary-menu'));
         } else { 
            wp_nav_menu(array('theme_location'=>'menu-2','menu_id'=>'primary-menu'));
         } 

Your Answer

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

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