在 BBPress 中添加话题(帖子)查看量计数器

翻译自这里,有一点修改

BBPress 是一款 WordPress 论坛插件(如 phpBB、Discuz! 等)。尽管 BBPress 没有像 phpBB 和 Discuz! 那样多的功能,但是它们的 SEO 分数要高于两者,而且 BBPress 是免费的。 BBPress 使用 WordPress 的固定链接,更亲和搜索引擎,很多站长都采用了 bbPress 作为他们的论坛软件。
在 bbPress 的帖子列表里,你一般能看到如下所示:

帖子 (Topic) 参与人数 (Voices) 回复 (Posts) 最后回复 (Freshness)
An Topic
Started by:
Me
2 3 1 minutes ago
Me

实际上并没有多少人关心有多少人回复了某个帖子,所以,

今天,我将向您展示如何将帖子查看量计数器添加到 BBPress 里


首先,确保你已经安装了 BBPress 插件,你可以在这里下载 BBPress 插件。

1. 添加计数器功能

打开你的主题(一般在 “htdocs(或者类似的地方)\wp-content\themes\your-theme(你目前使用的主题)”)里的 functions.php,添加以下代码(位置不限)(可以复制):

//把参与人数改成观看(点击)数量
//https://trickspanda.com/add-replies-topics-count-bbpress/
if( !function_exists('get_wpbbp_post_view') ) :
////////////////////////////////////////////////////////////////////////////////
// get bbpress topic view counter
// 获取 bbpress 帖子查看量计数器
////////////////////////////////////////////////////////////////////////////////
function get_wpbbp_post_view($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
}
function set_wpbbp_post_view($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if( $count == '' ){
        add_post_meta($postID, $count_key, '1');
    } else {
        $new_count = $count + 1;
        update_post_meta($postID, $count_key, $new_count);
    }
}
endif;

上面这段代码是干什么的?

这是添加查看计数器到主题元的功能。

2. 将挂钩(Hook)动作添加到每个帖子上

然后,在新添加的代码下面添加以下代码:

if( !function_exists('add_wpbbp_topic_counter') ) :
////////////////////////////////////////////////////////////////////////////////
// init the view counter in bbpress single topic
// 在 bbpress 单个帖子中 初始化查看量计数器
////////////////////////////////////////////////////////////////////////////////
function add_wpbbp_topic_counter() {
global $wp_query;
$bbp = bbpress();
$active_topic = $bbp->current_topic_id;
set_wpbbp_post_view( $active_topic );
}
add_action('bbp_template_after_single_topic', 'add_wpbbp_topic_counter');
endif;

3. 在模板中应用计数器

您可以在任何 bbpress 循环(Loop)中使用此代码:

<?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?>

在这里用我的论坛做例子,我想把 “参与人数” 替换成 “查看量”:

帖子 查看 回复 最后回复
A Topic
由 Me 发布
2 0 16 小时, 30 分钟 前
Me

使用 bbPress 的子模板方法,我在我的主题中创建了一个 bbpress 文件夹,并将loop-single-topic.php复制过去(该文件的位置:/wp-content/plugins/bbpress/templates/default/bbpress/loop-single-topic.php),然后编辑 mytheme(你的主题文件夹)/bbpress/loop-single-topic.php 第 82 行左右,从:

<?php bbp_topic_voice_count(); ?>

改为:

<?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?>

“查看量” 就替代了 “参与人数” 的位置。

搞定!

如果你只使用 bbPress 的话,上述教程应该能完美运作(创建一片帖子,进入几次并查看外面的查看量是否有变化)。

但是,

BuddyPress 群组里的论坛部分 并不会运作,具体现象为:

  • 点击任何帖子都不会增加查看量

若想修复,请重新打开你的主题里的 functions.php,并找到:
$active_topic = $bbp->current_topic_id;
改为:
$active_topic_id = $bbp->topic_query->post->ID;
即可。

参考资料:

view count in bbpress group forum doesn’t work


http://www.magpress.com/blog/adding-topics-view-counter-in-bbpress/