简体   繁体   中英

Wordpress: Conditional / Page Specific Menu

I'm creating a website with Divi builder. I'm currently editing the header.php file and I'm having problems with applying a conditional menu. I'm currently using the primary menu for one page and the secondary menu for the other. Both work perfectly fine in desktop view but in the mobile dropdown, all items from both primary and secondary appear.

if(is_page(1)){

$primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );             

} else if (is_page(2)){

$primaryNav = wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );

} else {

$primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );

}

I believe this is for the mobile

$slide_nav = '';
$slide_menu_class = 'et_mobile_menu';

$slide_nav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );
$slide_nav .= wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );


How is this drop down menu configured? maybe in your theme it is called by another function and actually only has items associated with it in admin. Check in the admin panel if there are no menus without being associated and if there is not yet put here an excerpt from where is the mobile menu to know if there is any coditional different from yours.

--------------Updated after update------------------

In your first code you can simplify it like this:

if (is_page(2)){

    $primaryNav = wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );

} else {

    $primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => 'top-menu', 'echo' => false ) );

}

In your second code you can do it like this:

$slide_nav = '';
$slide_menu_class = 'et_mobile_menu';

if (is_page(2)){
    $slide_nav .= wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );
} else {
    $slide_nav .= wp_nav_menu( array( 'theme_location' => 'primary-menu', 'container' => '', 'fallback_cb' => '', 'echo' => false, 'items_wrap' => '%3$s' ) );
}

This one has been resolved. Turns out the mobile nav was in the parent theme's functions.php

The solution was to add another menu in the register_nav_menus() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM