0

所以,我在这里为我的社区网站使用 Boombox 主题,我想显示来自用户的“总帖子阅读/浏览量”,例如,A 用户有 10 个帖子并且每个帖子都有不同的视图,我需要来自所有用户的总视图A的帖子。可以在wordpress平台上做吗?因为我在我的数据库表中找到了,所以我找不到任何我可以“玩”的相关字段。

我尝试修改 update_post_meta 函数,尝试在 function.php 文件中添加另一个条件,但它不起作用。

这是功能:

function boombox_update_post_total_view( $scale, $post_id ) {
    if( absint( $scale ) > 0 ) {
        $total = intval( boombox_get_post_meta( $post_id, 'total_views' ) );
        $total += $scale;

        update_post_meta( $post_id, 'total_views', $total);

    }
}

add_action( 'boombox/view_total_updated', 'boombox_update_post_total_view', 10, 2 );

这是数据库表结构: 数据库表结构

谢谢你。注意:我什至不确定我是否编辑了正确的文件。

4

2 回答 2

0

这是我现在使用的代码:)

 <?php
                                            $profile_id = bp_get_member_user_id();
                                            $balance    = mycred_get_users_balance( $profile_id );
                                            $author_id = ''; // do stuff to get user ID
                                            $author_posts = get_posts( array(
                                                'author' => $profile_id
                                            ) );

                                            $counter = 0; // needed to collect the total sum of views

                                            foreach ( $author_posts as $post )
                                            {
                                                $views = absint( boombox_get_post_meta( $post->ID, 'total_views', true ) );
                                                $counter += $views;
                                            }
                                        ?>
于 2018-07-04T07:13:05.163 回答
0

这适用于任何主题。

第 1 步:在您的主题function.php文件 中添加以下代码块中的代码。

function getPostViews($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 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

第 2 步:将这行代码添加到single.php文件中。请注意,它应该在循环内,您可以在之后添加它the_content()

setPostViews(get_the_ID());

例如,它应该是:

the_content(); setPostViews(get_the_ID());

第 3 步:在要显示帖子视图总数的位置添加这行代码:

echo getPostViews(get_the_ID());

来源:https ://www.themexpert.com/blog/track-display-post-views-in-wordpress

于 2018-07-04T04:24:26.163 回答