I have added my secondary menu on my function.php file and WordPress also allowed me to choose the secondary menu. The problem is both menu bar appeared at the same time so i have double menu bar on the top of my home page and also my about us page. What I'm try to do is show my primary menu bar only in my main home page, and show secondary menu on my about us page, services page, etc... How can i fix it?
function.php file
register_nav_menus( array(
'primary' => esc_html__( 'Primary', 'themename' ),
'secondary' => __( 'Secondary Menu', 'themename' ),
) );
Header.php file
<?php if ( has_nav_menu( 'primary' )
'theme_location' => 'secondary',
'container' => 'nav',
'container_class' => 'navbar navbar-default',
'menu_class' => 'nav navbar-nav navbar-right'
; ?>
<?php
if ( has_nav_menu( 'secondary_navigation_menu' ) ) {
wp_nav_menu( array( 'theme_location' => 'secondary_navigation_menu' ) );
} ?>
<?php if ( is_home() ): ?>
<?php if ( has_nav_menu( 'primary' ) : ?>
//primary menu
<?php endif; ?>
<?php endif; ?>
P.S. Both of my menu bar are different CSS.
Your help will be appreciated!
Use the built in conditionals Wordpress provides:
is_home()
- https://developer.wordpress.org/reference/functions/is_home/is_front_page()
- https://codex.wordpress.org/Function_Reference/is_front_pageIf you have set your home page to display your latest posts then you should use is_home()
, if you've set your home page as the front page through Reading > Settings > Front page displays
, then you should use is_front_page()
.
Wrap those menu functions in a conditional statement that's applicable to your case.
You could also combine them:
<?php
if ( !(is_front_page() || is_home()) ) {
wp_nav_menu( array(
'theme_location' => 'secondary',
'container' => 'nav',
'container_class' => 'navbar navbar-default',
'menu_class' => 'nav navbar-nav navbar-right'
) );
}
?>
The above conditional statement will return true
if the page in question isn't the home page or the front page.
You can also use the is_page()
conditional tag to check any other page.
This function allows you to pass an optional parameter in its parenthesis to target a specific page, or number of pages using (int|string|array) Page ID, title, slug, or array.
You can call the secondary menu in the page that you wish alone. By using the is_page() you can display the menu where ever you want.
// When any single Page is being displayed.
is_page();
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );
For Example if you are going to display secondary menu in all the pages except home
page you can have the code like this.
<?php
if (!is_home())
{
// Here you can display the Secondary menu
if ( has_nav_menu( 'secondary-menu' ) ) { /* if menu location 'secondary-menu' exists then use custom menu */
wp_nav_menu( array( 'theme_location' => 'secondary-menu') );
}
else
{
// here you can display the primary menu.
if ( has_nav_menu( 'primary-menu' ) ) { /* if menu location 'primary-menu' exists then use custom menu */
wp_nav_menu( array( 'theme_location' => 'primary-menu') );
}
?>
Now the primary menu will be displayed only in about-me page alone.
is_page() supports array too:
if( is_page( array( 'about-us', 'contact', 'management' ) )
//either in about us, or contact, or management page is in view
else
//none of the page about us, contact or management is in view
Replace your header.php
as i have provided below.
Your Header.php needs to be changed like this. You write all the other codes for registering the menu and others in function.php file don't mix it up here in header.php
file.
<?php
if(is_home() || is_front_page()) // This ensures that the below part execute for home page or front page alone
{
if ( has_nav_menu( 'primary-menu' ) ) {
/* if menu location 'primary-menu' exists then use custom menu */
wp_nav_menu( array( 'theme_location' => 'primary-menu') );
}
}
else
{
if ( has_nav_menu( 'secondary_navigation_menu' ) ) {
wp_nav_menu( array( 'theme_location' => 'secondary_navigation_menu' ) );
}
}
?>
If you go through your header.php there should be something like this .
if ( has_nav_menu( 'primary' ) : ?>
Now in your functions.php register another menu location
register_nav_menus( array(
'secondary_menu' => 'Secondary Navigation Menu',
'secondary_navigation_menu' => 'My Custom Footer Menu',
) );
and after if ( has_nav_menu( 'primary' ) : ?>
section there should be code to display menu something like
wp_nav_menu( array(
'theme_location' => 'secondary',
'container' => 'nav',.......
after this add
<?php
if ( has_nav_menu( 'secondary_navigation_menu' ) ) {
wp_nav_menu( array( 'theme_location' => 'secondary_navigation_menu' ) );
} ?>
now for displaying primary menu only in home page wrap is_home() like
<?php if ( is_home() ): ?>
<?php if ( has_nav_menu( 'primary' ) : ?>
//primary menu
<?php endif; ?>
<?php endif; ?>
if you want your primary menu not to appear on home page do like
<?php if ( ! is_home() ): ?>
<?php if ( has_nav_menu( 'secondary_navigation_menu' ) : ?>
//secondarymenu
<?php endif; ?>
<?php endif; ?>