I was trying to set up different menus based on what category "page" you were on using the built in Wordpress menu editor.

In my header file I set up:

    if (is_home()) {
    $menu = 'home';}

    $menu = 'aviation';}

    $menu = 'anti-terrorism';}
    $menu = 'asbestos';}
    $menu = 'burn-pits';}
    $menu = 'catastrophic-injury';}
    $menu = 'medical';}

    $menu = 'securities';}


And I was calling the menu like:

<?php wp_nav_menu( array(  'theme_location'  => "$menu" , 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>

I set up the functions.php file with:

function show_posts_nav() {
    global $wp_query;
    return ($wp_query->max_num_pages > 1);
add_action( 'init', 'register_my_menus' );


if ( function_exists( 'register_nav_menus' ) ) {
          'home' => 'The Home Menu',
          'aviation' => 'Aviation',
          'anti-terrorism' => 'Anti-Terrorism',
          'asbestos' => 'Asbestos',
          'burn-pits' => 'Burn Pits',
          'catastrophic-injury' => 'Catastrophic .Injury',
          'medical' => 'Medical',
          'securities' => 'Securities'


None of the menus are showing up however. Am I doing something wrong here? I know the body class is set to "class="archive category category-aviation category-5 logged-in admin-bar" when I am on the aviation page.

  • 1
    have you tried to use the conditional tag is_category() ? example: elseif( is_category('aviation') ){ $menu = 'aviation';}
    – Michael
    Jun 5, 2011 at 15:50
  • Thanks @michael. That worked like a charm. I appreciate the help! Jun 5, 2011 at 15:58
  • 1
    can you post what the final code looked like as an answer?
    – shawn
    Jun 5, 2011 at 21:43

1 Answer 1


Outlining your concept

As far as i can see, you want to do the following:

  1. Check if there is more than one page ($GLOBALS['wp_query']->max_num_pages > 1)
  2. If TRUE, add your nav menus

The concept fails in some points (more explanation at the end of the answer):

  1. You're questioning too early if there's more than one page. (Your idea really is to only show these menus if you're on page 2 of a paginated archive?)
  2. You're looking into $post->post_name without a) having the data available from the global b) outside the loop and c) from the wrong global

Make your life easier

In your functions.php file you can add the menu like this:

function wpse19269_add_nav_menus()
    add_theme_support( 'menus' );

    $nav_menus = array(
         'home'          => 'The Home Menu',
         'aviation'      => 'Aviation',
         'anti-terrorism'    => 'Anti-Terrorism',
         'asbestos'      => 'Asbestos',
         'burn-pits'         => 'Burn Pits',
         'catastrophic-injury'   => 'Catastrophic Injury',
         'medical'       => 'Medical',
         'securities'        => 'Securities'
    register_nav_menus( $nav_menus );
add_action( 'init', 'wpse19269_add_nav_menus', 20 );

If you're using the functions.php file, you can write your own custom functions and avoid cluttering your thems files:

function wpse19269_show_nav_menus()
    $menu = ''; // Set a value to avoid errors if no condition is met
    if ( is_home() )
        $menu = 'home';
    elseif( is_category('some_category') )
        $menu = 'whatever';
        // and so on...

    // Nav Menu
    wp_nav_menu( array( 
         'theme-location'   => $location
        ,'menu'         => 'top-nav'
        ,'menu_id'      => 'top-nav'
        ,'container'        =>  false
        ,'container_class'  => 'menu-header' 
    ) );
// Use it in your theme like this:
// wpse19269_show_nav_menus();

After reading through your snippets, I think it's best to explain some basic things:

Global Variables

If you want to access global available $variables, you'll have to first call them as global $whatever before you use it inside your function.


function the_example()
    global $post; // Now we have the global data available in the function
    echo $post->post_title; // This is the post title - must be used inside the loop

You can read here more about it.

Interacting with data in hooks

Please take a look at the Plugin API/Action Reference to see when $wp_query is ready and available. You call on the init hook is too early.

Conditional Tags

Use other Conditional Tags, like is_category('see codex link for more information');.

Some other notes regarding your code:

wp_nav_menu( array( 'theme_location' => "$menu" ... should be written without the ".

WordPress functions

You don't have to question if ( function_exists('some_native_wp_fn') ) {, because it will exist. If doesn't exist anymore, you'll get a depracated notice that tells you about it. If you question the existance you suppress the notice and won't be able to fix it (or have a hard time finding it).

Your Answer

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

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