WordPress功能使我的菜单消失 [英] Wordpress function makes my menu disappear
问题描述
我正在使用此功能使我的自定义帖子类型投资组合"显示在档案/类别/标签页上(在functions.php中):
I am using this function to make my custom post type "portfolio" show up on archives/category/tag pages (in functions.php):
function namespace_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array('post', 'portfolio'));
return $query;
}
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types'
问题是由于某种原因,这使我的导航菜单消失了.这是导航菜单的代码(在header.php中):
The problem is that for some reason it is making my nav menu disappear. Here is the code for the nav menu (in header.php):
<?php wp_nav_menu(array( 'theme_location' => 'primary', 'sort_column' => 'menu_order', 'menu_class' => 'nav-menu', 'container_class' => 'nav-menu',) ); ?>
知道我可以改变什么吗?
Any idea what I can change?
推荐答案
WordPress导航菜单由 nav_menu_item
帖子类型的帖子组成,随着函数更改所有查询的帖子类型,无内容显示.
WordPress nav menus consist of posts of the nav_menu_item
post type, and as your function changes the post type for all queries, there is nothing to display.
解决方案:通过选中 is_main_query
仅修改主查询,例如:
Solution: modify only the main query by checking is_main_query
, e.g.:
if( is_category() && $query->is_main_query() ) {
// do stuff
}
PS: pre_get_posts
是一个 action 挂钩,因此您应该使用 add_action
而不是 add_filter
:>
PS: pre_get_posts
is an action hook, so you should use add_action
instead of add_filter
:
add_action( 'pre_get_posts', 'namespace_add_custom_types' );
这篇关于WordPress功能使我的菜单消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!