Why do I have a weird rendering issue only on 404.php caused by wp_nav_menu?

I built a navigation menu using the native menu builder in WordPress 3. This is the code I use to render it:

<?php wp_nav_menu( array('menu' => 'Main Navigation','menu_id'=> 'headernav','menu_class'=>'navigate')); ?>

It renders exactly the way I would expect on every page except 404.php. This is how it renders everywhere else:

Read More
<div class="menu-main-navigation-container"><ul id="headernav" class="navigate">

and this is how it renders on 404.php:

<div class="navigate"><ul>

And on 404.php it lists out every single page on my site, as opposed to the ones I built into the menu.

Does anybody have any idea why this is happening? Any help is greatly appreciated.

EDIT

Here is the code from my functions.php file:

function mytheme_setup() {

    register_nav_menus( array(
        'main-navigation' => 'Main Navigation',
        'subnav-navigation' => 'Sub menu nav',
        'how-eli-works-nav' => 'How Eli Works'
    ) );
}
add_action( 'after_setup_theme', 'mytheme_setup' );

I also did as you suggested and went back to the Menus panel and assigned theme locations. Same result: things work where they did before, and 404.php is still broken.

This is the code calling the menu from my header.php file:

<?php wp_nav_menu( array('theme_location' => 'main-navigation', 'menu' => 'Main Navigation','menu_id'=> 'headernav','menu_class'=>'navigate')); ?>

And this is the code from my 404.php file:

<?php
get_header(); ?>


    <div id="pagecontent" class="narrowcolumn" role="main">


            <article id="post-0" class="post error404 not-found">
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Oops, we couldn&rsquo;t find what you requested.', 'twentyeleven' ); ?></h1>
                </header>

                <div class="entry-content">
                    <p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching, or one of the links below, can help.', 'twentyeleven' ); ?></p>

                    <?php get_search_form(); ?>

                    <?php the_widget( 'WP_Widget_Recent_Posts', array( 'number' => 10 ), array( 'widget_id' => '404' ) ); ?>

                    <div class="widget">
                        <h2 class="widgettitle"><?php _e( 'Most Used Categories', 'twentyeleven' ); ?></h2>
                        <ul>
                        <?php wp_list_categories( array( 'orderby' => 'count', 'order' => 'DESC', 'show_count' => 1, 'title_li' => '', 'number' => 10 ) ); ?>
                        </ul>
                    </div>

    </div>

<?php get_footer(); ?>

Related posts

Leave a Reply

2 comments

  1. I’m not sure that this would be causing your problem, but one way you’re definitely Doing It Wrong is referencing menu instead of theme_location in your call to wp_nav_menu().

    Let’s assumed you’ve registered your menu as “main-navigation” in functions.php, like such:

    register_nav_menus( array(
        'main-navigation' => 'Main Navigation',
        'secondary-navigation' => 'Secondary Navigation'
    ) );
    

    …then your wp_nav_menu() should reference this registered theme_location:

    wp_nav_menu( array(
        'theme_location' => 'main-navigation',
        'menu_id'=> 'headernav',
        'menu_class'=>'navigate'
    ) );
    

    There is no inherent reason that an error 404 template page should output the nav menu any differently than any other template. If the above doesn’t fix the problem, then we’ll need to see more of your 404.php markup, to diagnose the problem.

    EDIT

    Okay, I tried registering it and it had an effect: now the menu just doesn’t register.

    Can you update your answer, and post your register_nav_menus() code?

    Note: ensure that this function call is hooked into after_setup_theme; it normally goes in a theme_setup() function, hooked into after_setup_theme, like such:

    function mytheme_setup() {
    
        // Register Nav Menu Locations
        register_nav_menus( array(
            'main-navigation' => 'Main Navigation',
            'secondary-navigation' => 'Secondary Navigation'
        ) );
    
        // Other Theme-setup functions may go here
    }
    add_action( 'after_setup_theme', 'mytheme_setup' );
    

    So, post that code in your answer.

    Also, once you’ve registered your Theme Locations, you need to go to Dashboard -> Appearance -> Menus, and ensure that you have applied your custom menu to the appropriate Theme Location.

    EDIT 2

    This is the code calling the menu from my header.php file:

    <?php wp_nav_menu( array('theme_location' => 'main-navigation', 'menu' => 'Main Navigation','menu_id'=> 'headernav','menu_class'=>'navigate')); ?>
    

    You are still passing menu and menu_id parameters to wp_nav_menu().

    Why are you doing that? As I said above, that is Doing It Wrong. You need to pass only the theme_location parameter to wp_nav_menu(). WordPress then applies whatever menu is assigned via Dashboard -> Appearance -> Menus.

    And this is the code from my 404.php file:

    I’m not sure if it’s related, but your HTML markup is malformed. You have an opening <article> tag, and no corresponding, closing </article> tag.

  2. This isn’t an answer necessarily, but after some more research I found these threads:

    http://wordpress.org/support/topic/custom-menu-not-displaying-on-404-error-page

    http://www.prettyscripts.com/software/wordpress/wordpress-custom-menu-not-displayed-on-404-page

    There is some conflict or bug that causes custom menus to not render correctly on 404 pages. Changing permalinks to not use any numbers (for example, /%category%/%postname%/ ) causes the menus to render correctly.

    Hopefully something they’ll fix in an upcoming release :/