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
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.