【问题标题】:Alter the Main WP_Comment_Query (comments at the bottom of posts)?更改主 WP_Comment_Query(帖子底部的评论)?
【发布时间】:2016-05-08 07:35:22
【问题描述】:

我的插件通过我自己的 WP_Comment_Query 检索了帖子开头的几个 cmets。我保存了这些 ID,以便随后可以更改 WP_Comment_Query 请求而不获取这些 ID。

当我使用 pre_get_cmets 挂钩隐藏这些已获取的 ID 时,它们也会在我每篇文章开头的第一个查询中隐藏。它违背了这一点。

$this->loader->add_action( 'pre_get_comments', $plugin_public, 'hide_the_comments' );

public function hide_the_comments( $comment_query ) {

    $comment_query->query_vars['comment__not_in'] = $the_ids_to_hide;
}

我们如何才能只针对底部请求,就像 post 循环的 is_main_query() 一样?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:
    • 创建一个私有变量,例如private $count = 0;
    • 每次运行函数时增加它
    • 如果您是第一次运行 cmets,请不要隐藏它 :)

    【讨论】:

    • 可以工作,但我运行的 wp_comment_query 数量不可预测。我改变了我的方法,而不是使用 pre_ 钩子,我只是使用了 comment_template 钩子,并用自己的布局和所有的一切运行了我自己的查询。
    【解决方案2】:

    如果您需要在 comments_template() 核心函数中定位“主”WP_Query_Comments(),那么自 WordPress 4.5 起,comments_template_query_args 过滤器就是 available

    $comment_args = apply_filters( 'comments_template_query_args', $comment_args );
    $comment_query = new WP_Comment_Query( $comment_args );
    

    请参阅票证#34442 了解更多信息和一个简单示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2023-04-10
      • 2022-01-10
      相关资源
      最近更新 更多