最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Same comment section on every page

programmeradmin0浏览0评论

I'm looking to basically have a unified comment section, so if a user posts on page a, it also shows on page b.

I've searched online but have been unable to find any documentation on this and wondered if it is easy to achieve as I'm totally new to wordpress coding etc but feel it can't be too difficult to pull up the same comment section on every page but I could be wrong!

I'm looking to basically have a unified comment section, so if a user posts on page a, it also shows on page b.

I've searched online but have been unable to find any documentation on this and wondered if it is easy to achieve as I'm totally new to wordpress coding etc but feel it can't be too difficult to pull up the same comment section on every page but I could be wrong!

Share Improve this question asked Aug 26, 2020 at 21:41 Fletch ArmstrongFletch Armstrong 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

With some testing this appears to be achievable with two filters.

Firstly you want to set front end queries for comments to query for comments for all posts, instead of just the current post. That can be done like this:

add_action(
    'pre_get_comments',
    function( $comment_query ) {
        if ( ! is_admin() ) {
            $comment_query->query_vars['post_id'] = 0;
        }
    }
);

However some logic in comment templates will be based on the number of comments that the WordPress thinks the post has. This is stored on each post as comment_count separate to the comment query for performance reasons, so just changing which comments are queried won't affect this number. However, with the get_comments_number filter we can replace each post's comments count with the total number of comments on the site, like this:

add_filter(
    'get_comments_number',
    function( $count ) {
        if ( ! is_admin() ) {
            $count = wp_count_comments()->approved;
        }

        return $count;
    }
);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论