I want to show different menus for different templates. For example if the user is in any page i want to show one menu, if user is in post category i want to show another menu, if the user in single.php page i want to show another menu. How to do it ?
I know about creating multiple header(and these headers show different menu) in the theme and calling these header in different templates. But i want to do it simply though if else statement in my header.php itself. How to do it?
My header.php menu code is –
<div id="navbar" class="navbar-collapse collapse">
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'header-menu',
'depth' => 2,
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
</div>
i want to do an if else statement in the header.php itself like –
<div id="navbar" class="navbar-collapse collapse">
if(template is page)
{
//menu code 1
}
else (template is category)
{
//menu code 2
}
</div>
Please help ?
There are a few functions you could try that let you know what template you are using, as you want to do it conditionally
is_page_template
https://developer.wordpress.org/reference/functions/is_page_template/ could be the best one for you. You can then use that to show the relevant menu if a certain template is being used. Don’t forget a general else condition at the end to show a default menu 🙂