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 |4 Answers
Reset to default 8I 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
版权声明:本文标题:categories - Main menu not appearing on category pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736747118a1950839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'menu'
parameter from yourwp_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