My goal is to have a custom menu in the header based on the category of the post being viewed.
I'm setting up a single wordpress site that will contain about a half dozen different categories. Each category is going to be a manual I'm responsible for maintaining. I view wordpress as a much improved delivery method for this content than printed manuals or simply downloaded PDFs.
Based on the thread found at: http://wordpress.org/support/topic/custom-menusheaders-for-different-categories (specifically the fourth post) I've altered the header.php file in my theme folder by replacing the following code:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>
with this code:
<?php $cat = get_the_category();
$catSlug = $cat[0]->slug;
$catMenu = $catSlug . "-nav-menu";
wp_nav_menu(array(
'container'=>'nav',
'container_class'=>'menu-header',
'theme_location'=>'primary',
'menu'=>'{$catMenu}'
)
); ?>
I've created custom menus that have names matching the category slug followed by "-nav-menu". For example, the category Employee Handbook has a slug of employee-handbook and specific nav menu of employee-handbook-nav-menu.
I can echo out $catMenu and it displays exactly what I would expect.
The nav menu in the header is however the default nav menu, not the one specific to the category.
Does anyone have a suggestion as to why my wp_nav_menu call isn't performing the way I'm expecting? If anyone could point me in the right direction to accomplish my goal, I'd greatly appreciate it.
Thank you very much.
Per the request in the first comment, here is the output as found in view source:
<nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation">
<button class="menu-toggle">Primary Menu</button>
<a class="screen-reader-text skip-link" href="#content">Skip to content</a>
<ul id="menu-docs-navigation" class="menu">
<li id="menu-item-4" class="menu-item menu-item-type-taxonomy menu-item-object-category current-menu-item menu-item-4"><a href="http://jaredtesta.net/main/category/flight-operations-manual/">Flight Operations Manual</a></li>
<li id="menu-item-5" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-5"><a href="http://jaredtesta.net/main/category/standardization-manual/">Standardization Manual</a></li>
<li id="menu-item-6" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6"><a href="http://jaredtesta.net/main/category/sopa/">Standard Operating Procedures Amplifier</a></li>
</ul>
</nav>
Edit: Found the solution. You do not need either the single quote marks or the curly brackets around the variable in wp_nav_menu call. Example below:
<?php $cat = get_the_category();
$catSlug = $cat[0]->slug;
$catMenu = $catSlug . "-nav-menu";
$options = array(
'container'=>'nav',
'container_class'=>'menu-header',
'theme_location'=>'primary',
'menu'=>$catMenu
);
wp_nav_menu($options); ?>