0

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); ?>
4
  • Can you explain/show how the wp_nav_menu code is displaying on your site and how that differs from what you're expecting? It is difficult to identify what is going wrong without the output.
    – Stephen S.
    Jun 6, 2014 at 19:01
  • OK. I got it figured out. You do not need the single quote marks or the curly brackets around the variable. Passing just the variable is fine. I'll edit the post with the correct code so others in the future can use this post as reference. Thank you for your help.
    – jtesta
    Jun 7, 2014 at 17:55
  • ah good, that was what I was thinking was the issue as well. glad you got it working!
    – Stephen S.
    Jun 7, 2014 at 19:29
  • Please post your solution as an answer and come back in two days and accept it. Jun 7, 2014 at 19:58

1 Answer 1

1

As indicated above, I discovered the answer to my own question. You do not need single quotes or brackets around the variable. The fact that the variable is a string itself is sufficient.

The correct code is 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); ?>

I've also discovered a multiple themes plugin located here: http://jonradio.com/plugins/jonradio-multiple-themes/

This would allow different themes for different categories. It may be overkill to create new themes just to keep the menus different, but if you're looking for changing headers, menus, and layouts it may be what you're looking for.

1
  • Thanks for posting, just remember to visit your question again in two days to accept it Jun 9, 2014 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.