我在过去的3个小时里一直在网上搜索,但没有成功。
我有一个网站,作者可以在那里发布帖子,帖子视图可以使用Posteta“post\\u views\\u count”进行跟踪。
我正在使用此代码和作品,但只显示前5篇文章的数量,而不是所有作者的文章。这是密码?
global $wp_query;
$author_id = get_current_user_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 ) );
var_dump($views);
$counter += $views;
}
echo "{$counter}";
怎么了?为什么只显示5个帖子的数量?非常感谢
SO网友:ahmet
多亏了汤姆·诺威尔的回答,我才明白了这一点。
以下是缺失的部分:
$author_posts = get_posts( array(\'author\' => $author_id, \'numberposts\' => -1 ));
试试这个
<?php
global $wp_query;
$author_id = $wp_query->queried_object_id;
$author_posts = get_posts( array(\'author\' => $author_id, \'numberposts\' => -1 ));
$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;
?>