获取当前作者文章总浏览数量和指定作者文章总浏览数量
获取指定作者文章总浏览数
if(!function_exists('cx_posts_views')) {
function cx_posts_views($author_id = 1 ,$display = true) {
global $wpdb;
$sql = "SELECT SUM(meta_value+0) FROM $wpdb->posts left join $wpdb->postmeta on ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE meta_key = 'views' AND post_author =$author_id";
$comment_views = intval($wpdb->get_var($sql));
if($display) {
echo number_format_i18n($comment_views);
} else {
return $comment_views;
}
}
}
代码调用
/**
*文章页面直接输出该作者文章的浏览总数
*千级浏览数量会用逗号分隔
**/
//方法一
cx_posts_views(get_the_author_meta('ID'));
//方法二
$author_id = get_the_author_meta('ID');
cx_posts_views($author_id);
//如果我们只需要获取数量不做格式处理可以用
$author_id = get_the_author_meta('ID');
$author_views = cx_posts_views($author_id,false);
echo $author_views;
//或者
$author_views = cx_posts_views(get_the_author_meta('ID'),false);
echo $author_views;
//来调用