【问题标题】:WordPress pre_get_posts causing custom menu to failWordPress pre_get_posts 导致自定义菜单失败
【发布时间】:2017-08-30 18:01:55
【问题描述】:

我正在尝试使用 pre_get_posts 函数来定位标签模板,以将帖子类型添加到查询中。我使用的代码看起来超级基本,应该没问题。但是,它没有并且正在破坏查询,更重要的是,它以某种方式破坏了自定义菜单,使其无法仅在该标记模板上返回。

function tags_support_query($query) {
    if ( is_tag() ) {
        $query->set('post_type', 'any');
    }
}

add_action('pre_get_posts', 'tags_support_query');

提前感谢您的任何意见/建议。

【问题讨论】:

  • 当你说“打破查询”时,你到底是什么意思?

标签: php wordpress


【解决方案1】:

在 if 语句中添加$query->is_main_query() 约束,如下所示:

function tags_support_query($query) {
    if( is_tag() && $query->is_main_query() ) {
        $query->set('post_type', 'any');
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多