Reputation: 4536
I have 2 menus that I've registered in my functions.php
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', __( 'Primary Menu', 'visio' ) );
register_nav_menu( 'onepage', __( 'One Page Menu', 'visio' ) );
}
The One Page Menu
should only display on the frontpage, which is using a custom page template template-parts/page-homepage.php
And the Primary Menu
should display on all other pages.
However, when I create the if condition in my header.php
- it doesn't work, it always displays the One Page Menu
on the frontpage and on all other pages.
if(is_page_template('template-parts/page-homepage.php')) {
wp_nav_menu(
array (
'menu' => 'onepage',
'container' => false,
'container_id' => false,
'menu_class' => 'nav navbar-nav',
'menu_id' => false,
'depth' => 2,
'walker' => new Description_Walker
)
);
} else {
wp_nav_menu(
array (
'menu' => 'primary',
'container' => false,
'container_id' => false,
'menu_class' => 'nav navbar-nav',
'menu_id' => false,
'depth' => 2,
'walker' => new Description_Walker
)
);
}
What am I doing wrong? And how can I make this work?
Upvotes: 1
Views: 1578
Reputation: 9703
There are few ways that comes to my mind.
1) Using separate header.php template for home page and other page .
2) Using menu location settings .
3) Using setting options and according to them display the menu
But with your code I think you should try replace is_page_template('template-parts/page-homepage.php')
with
is_page_template('page-homepage.php')
As
is_page_template() is not going for directory .
function is_page_template( $template = '' ) {
if ( ! is_page() )
return false;
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) )
return (bool) $page_template;
if ( $template == $page_template )
return true;
if ( is_array( $template ) ) {
if ( ( in_array( 'default', $template, true ) && ! $page_template )
|| in_array( $page_template, $template, true )
) {
return true;
}
}
return ( 'default' === $template && ! $page_template );
}
2 ) When using menu location setting you can use
register_nav_menus( array(
'pluginbuddy_mobile' => 'PluginBuddy Mobile Navigation Menu',
'footer_menu' => 'My Custom Footer Menu',
) );
and
<?php
if ( has_nav_menu( 'footer_menu' ) ) {
wp_nav_menu( array( 'theme_location' => 'footer_menu' ) );
} ?>
to control the display of menu.
3) With setting options you can do something use update_option()
and get_option()
.
Upvotes: 2