WordPress的pre_get_posts类别过滤器删除自定义菜单项 [英] Wordpress pre_get_posts category filter removes custom menu items
问题描述
它们是在函数中使用此代码创建的.php;
函数register_my_menus(){
register_nav_menus(
array(
'header -menu'=> __('Header Menu'),
'extra-menu'=> __('Extra Menu')
)
);
}
add_action('init','register_my_menus');
这是我使用菜单的代码;
< nav>
<?php wp_nav_menu(array('theme_location'=>'header-menu'))?>
< / nav>
< nav id =ecommerce>
<?php wp_nav_menu(array('theme_location'=&'extra-menu')); ?>
< / nav>
菜单工作正常,除非您转到侧栏中的类别,例如文章或博客页面上的活动;
http ://www.ducklingfarm.com/blog/
博客页面是一个自定义发布类型,为了使该类别正常工作,我添加了一些代码进入functions.php,从那时起菜单不能正常工作。该代码是;
$ $ p $ add_filter('pre_get_posts','query_post_type');
函数query_post_type($ query){
if(is_category()|| is_tag()){
$ post_type = get_query_var('post_type');
if($ post_type)
$ post_type = $ post_type;
else
$ post_type = array('post','Blog');
$ query-> set('post_type',$ post_type);
返回$ query;
$ b $ p
$ b所以我猜测代码有问题。请帮帮我!我真的很感激。
Best,
Jaeeun
我通过更改
add_filter('pre_get_posts','query_post_type');
函数query_post_type($ query){
if(is_category()&& $ query-> is_main_query()){
$ post_type = get_query_var('post_type');
if($ post_type)
$ post_type = $ post_type;
else
$ post_type = array('post','Blog');
$ query-> set('post_type',$ post_type);
返回$ query;
$ div $解析方案
你可以试试(如果和 $ post_type = $ post_type;
)不需要多个
add_filter('pre_get_posts','query_post_type');
函数query_post_type($ query){
if(is_category()&& $ amp; $ query-> is_main_query()){
$ query-> set('post_type',array ('post','Blog'));
}
return $ query;
}
So I have this site where you can see there are two menus, one next to the logo the other on top right;
They are created using this code in functions.php;
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
and this is my code to use the menus;
<nav>
<?php wp_nav_menu(array( 'theme_location' => 'header-menu' ) ) ?>
</nav>
<nav id="ecommerce">
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
</nav>
And the menus work fine, except when you go to categories in the sidebar such as "Articles" or "Events"on the "Blog" page;
http://www.ducklingfarm.com/blog/
The blog page is a custom post type, and to make the category work, I added some code into functions.php, and the menus are not working properly since then. That code is;
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('post','Blog');
$query->set('post_type',$post_type);
return $query;
}
}
So I'm guessing there's something wrong with the code. Please help me! I'd really appreciate it.
Best, Jaeeun
I solved it by changing the last code to this;
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() && $query->is_main_query()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('post','Blog');
$query->set('post_type',$post_type);
return $query;
}
}
You may try this (No need for multiple if
and $post_type = $post_type;
)
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() && $query->is_main_query()) {
$query->set( 'post_type', array( 'post', 'Blog' ) );
}
return $query;
}
这篇关于WordPress的pre_get_posts类别过滤器删除自定义菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!