4

Im creating a theme based on blankslate/Bootstrap and having an issue with the main nav appearing on category pages (seems to appear everywhere else ok.).

In header.php:

<div class="collapse navbar-collapse" id="navbar-collapse-1">
                      <?php
                      wp_nav_menu( array(
                      'menu'              => 'primary',
                      'theme_location'    => 'primary',
                      'depth'             => 2,
                      'menu_class'        => 'nav navbar-nav',
                      'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                      'walker'            => new wp_bootstrap_navwalker())
                      );
                      ?>

In Functions.php

// Register Custom Navigation Walker
require_once('wp_bootstrap_navwalker.php');
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'themename' ),
) );

Any help or pointers would be appreciated

  • 2
    A few things: 1) remove the 'menu' parameter from your wp_nav_menu() call. Only use 'theme_location'. 2) What is your "category page"? The category archive index (category.php) or a custom page template? Or something else? 3) Can we see a live link that demonstrates the issue? – Chip Bennett Apr 25 '14 at 15:16
  • You need to show your walker code. – vancoder Apr 25 '14 at 17:23
  • Check CSS to see if there are some required elements to display menu. The "collapse" in menu ID is suspect. – unifiedac Apr 25 '14 at 19:01
  • Ive taken the site out of maintenance mode, reallybadmovies.co.uk so, if you click on any of the menu items bar the sample page (just to demo works ok on others). the main nav not appearing. I think the CSS is ok, as a look at the HTML shows me that the menu items arent actually being generated. My category page is category.php, and isnt a custom one Regarding the walker nav, its just using the settings as per github.com/twittem/wp-bootstrap-navwalker I removed the main menu reference as suggested, and problem still persists unfortunately – Pilgrimiv Apr 26 '14 at 14:30
7

I know this question is quite old but since there is no answer yet an there are many similar questions on the WordPress support forums, I'll better share my findings and maybe help someone.

The issue of disappearing menu may be caused by code in plugin or theme incorrectly modifying global $wp_query object using pre_get_posts filter hook.

I discovered that the code causing issues was in theme:

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    $query->set( 'post_type', array(
                'post',
                'projects',
            ));
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

This snippet is supposed to add custom post types to category archives but it also affected query in wp_nav_menu.

To fix the issue, I had to correct the if condition:

function namespace_add_custom_types( $query ) {
  if( is_archive() && (is_category() || is_tag()) && empty( $query->query_vars['suppress_filters'] ) ) {

    $query->set( 'post_type', array(
                'post',
                'projects',
            ));
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

YMMV and the cause could be entirely different, but this is how I fixed the issue of missing menu in category template.

1

For me

    if ($q->get('post_type') != 'post') {
        return;
    }

in the handler of pre_get_posts fixed it.

0

I had the same problem because I was adding the filter pre_get_posts and missing to pass the menu item to the post. This is how I solved:

Before, I had this code in my theme's functions.php wich I was overwriting the main query with the $post_type only, whitout passing the menu items, and the menu was not appearing:

add_filter( 'pre_get_posts', 'query_post_type' );
function query_post_type( $query ) {
    if ( is_category() ) {
        $post_type = get_query_var( 'post_type' );
        if ( $post_type ) {
            $post_type = $post_type;
        } else {
            $post_type = array( 'nav_menu_item', 'post', 'departments' );
        }
        $query->set( 'post_type', $post_type );

        return $query;
    }
}

And I solved passing the 'nav_menu_item' as follow:

add_filter( 'pre_get_posts', 'query_post_type' );
function query_post_type( $query ) {
    if ( is_category() ) {
        $post_type = get_query_var( 'post_type' );
        if ( $post_type ) {
            $post_type = array( 'nav_menu_item', $post_type );
        } else {
            $post_type = array( 'nav_menu_item', 'post', 'departments' );
        }
        $query->set( 'post_type', $post_type );

        return $query;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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