Displaying Total Author View

时间:2015-01-14 作者:Oli Price-Bates

我希望在其作者页面上显示每个作者生成的总视图。我找不到任何有效的php代码。有人能帮忙吗?

我已尝试使用此代码:

<?php
$author_posts = get_posts( array(\'author\' => $current_user->ID) );

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

foreach ( $author_posts as $post ) {

    $views = absint( get_post_meta( $post->ID, \'post_views_count\', true ) );
    $counter += $views;
}
echo "{$counter}";
?>
但显示的数字保持在“0”上。我是不是做错了什么?以下是我的一个作者页面:http://fresharsenal.com/author/olipricebates/

非常感谢您的帮助!如果有人知道如何显示生成的股份数量,那将是ace

1 个回复
SO网友:darrinb

这个$current_user 对象检索当前登录的用户。您需要的是您正在查看其页面的作者的ID。尝试以下操作:

global $wp_query;
$author_id = $wp_query->queried_object_id;

$author_posts = get_posts( array(\'author\' => $author_id) );

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

foreach ( $author_posts as $post ) {

    $views = absint( get_post_meta( $post->ID, \'post_views_count\', true ) );
    $counter += $views;
}

echo "{$counter}";
回显视图,查看是否有:

foreach ( $author_posts as $post ) {

    $views = absint( get_post_meta( $post->ID, \'post_views_count\', true ) );

    var_dump($views);

    $counter += $views;
}

结束

相关推荐