过滤自定义分类查询时,主菜单消失

时间:2014-02-08 作者:ptf

I have created a custom taxonomy, and that works.

But here is the problem;I have created an archive page, which in the url gets e.g. this: ?category=23. Then, in functions.php, I do this:

function add_inspiration_category( $q ) {
    if (is_post_type_archive(\'inspiration\') && !is_admin()) {

        $category_id = $_GET[\'category\'];
        if (!empty($category_id)) {
            $q->set(\'tax_query\', array(
                array(
                    \'taxonomy\' => \'inspiration-taxonomy\',
                    \'field\' => \'id\',
                    \'terms\' => $category_id
                )
            ));
        }
    }

    return $q;
}
add_filter(\'parse_query\', \'add_inspiration_category\');

So, basically I\'m filtering the archive pages result based on the id in the url. This works, except when I\'m filtering on a category, the main menu dissapears from the header. If I remove the ?category=23 from the url or comment out the section above where I set the tax_query, it works as expected again. Anyone know what this is?

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您正在将该筛选器应用于页面上匹配条件或if. 您需要将此限制为仅主查询,并确保条件仅在$q 传递给函数的查询。

if ($q->is_main_query() 
   && $q->is_post_type_archive(\'inspiration\') 
   && !is_admin()) {

结束

相关推荐

从wp-Query获取数据,在循环之外&不更改url

我正在基于自定义分类法在wordpress上进行高级搜索。我已经被困72小时了,所以我希望能得到一些帮助或思考。。。步骤1——在js文件中,查询字符串如下所示:if (jQuery(\'#s\').val() == \'\'){ URL = \"/?genre=\" + genre + \'...other Stuff\' #content\'; }else{ URL = \"/?s=\"+searchQueryString+\"&genre=\" + genre +\'.