如何在WordPress社区主题中显示用户的全部帖子视图?
所以,我在这里使用我的社区网站的Boombox主题,我想显示一个用户的“总文章阅读/浏览量”,例如一个用户有10篇文章,每个文章都有不同的浏览量,我需要一个用户所有文章的总浏览量。可以在wordpress平台上实现吗?因为我在数据库表中找到了什么,所以我找不到任何可以“玩”的相关字段 我试图修改一个update\u post\u元函数,试图在function.php文件中添加另一个条件,但它不起作用 下面是函数:如何在WordPress社区主题中显示用户的全部帖子视图?,wordpress,post-meta,Wordpress,Post Meta,所以,我在这里使用我的社区网站的Boombox主题,我想显示一个用户的“总文章阅读/浏览量”,例如一个用户有10篇文章,每个文章都有不同的浏览量,我需要一个用户所有文章的总浏览量。可以在wordpress平台上实现吗?因为我在数据库表中找到了什么,所以我找不到任何可以“玩”的相关字段 我试图修改一个update\u post\u元函数,试图在function.php文件中添加另一个条件,但它不起作用 下面是函数: function boombox_update_post_total_view(
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 );
以下是数据库表结构:
多谢各位。
注意:我甚至不确定我是否编辑了正确的文件。这适用于任何主题 第1步: 在themesfunction.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());
第3步:将这行代码添加到要显示post视图总数的位置:
echo getPostViews(get_the_ID());
来源:这是我现在使用的代码:)
okaaay,我不知道它是否按我想要的方式工作,但我已经找到了我想要的,我感谢你的回答,谢谢:)
echo getPostViews(get_the_ID());
<?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;
}
?>