admin管理员组

文章数量:1129441

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

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

Share Improve this question asked Apr 25, 2014 at 15:04 PilgrimivPilgrimiv 552 silver badges5 bronze badges 4
  • 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 Commented Apr 25, 2014 at 15:16
  • You need to show your walker code. – vancoder Commented Apr 25, 2014 at 17:23
  • Check CSS to see if there are some required elements to display menu. The "collapse" in menu ID is suspect. – unifiedac Commented Apr 25, 2014 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 Commented Apr 26, 2014 at 14:30
Add a comment  | 

4 Answers 4

Reset to default 8

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.

For me

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

in the handler of pre_get_posts fixed it.

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;
    }
}

I also had this problem and found out that the error is caused by this code snippet

add_action( 'pre_get_posts', function ( $query ) {
if ( !is_admin() ) {

    if( is_post_type_archive( 'project' ) && 'project' === $query->query['post_type'] ){
        $cur_date = date('Ymd', time());

        $query->set( 'order', 'ASC' );
        $query->set( 'orderby', 'menu' );
    }
    if(is_category()){
        $query->set('post_type', array('post', 'collections', 'tribe_events'));
    }
}}, 10, 2 );

The changes made include adding the condition $query->is_main_query() to ensure it's the main query on the page and checking empty($query->query['post_type']) before setting the post_type to avoid overwriting existing settings if they are already specified in the query. These adjustments should help prevent conflicts and ensure proper navigation on category pages.

Here is the full working code that helped me:

add_action('pre_get_posts', function ($query) {
if (!is_admin() && $query->is_main_query()) {

    if (is_post_type_archive('project') && 'project' === $query->query['post_type']) {
        $cur_date = date('Ymd', time());

        $query->set('order', 'ASC');
        $query->set('orderby', 'menu');
    }

    if (is_category() && empty($query->query['post_type'])) {
        $query->set('post_type', array('post', 'collections', 'tribe_events'));
    }
}}, 10, 1);

本文标签: categoriesMain menu not appearing on category pages