本页目录

在开发一些多人在线的网站时,为了凸显一些成员的勤奋,需要将他们发布的日志和评论进行展现,这一节的 WordPress 教程就和大家分享下,怎么获取指定用户的日志和评论数。
获取日志数
- 代码来源:详情
将以下代码添加至主题根目录下的 functions.php 文件<?下面:
/* number of author's posts by zwwooooo | zww.me */ function num_of_author_posts($authorID=''){ //根据作者ID获取该作者的文章数量 if ($authorID) { $author_query = new WP_Query( 'posts_per_page=-1&author='.$authorID ); $i=0; while ($author_query->have_posts()) : $author_query->the_post(); ++$i; endwhile; wp_reset_postdata(); return $i; } return false; }调用:
<?php echo num_of_author_posts($authorID); ?>例子:
<?php echo num_of_author_posts('1'); ?>显示 ID 为 1 的作者所发布的日志数量
说明:$authorID 获取方法就很多了,各个页面获取方式不同,自行研究,一般就这几个函数 get_the_author_meta(), get_userdata() ... 具体去 WordPress 官方查看
调用评论:
- 代码来源:详情
在需要调用的地方添加以下代码:
<?php $args = array( 'post_author' => '1' // fill in post author ID );
$author_comments = get_comments($args);
echo count($author_comments);?>该代码会显示用户 ID 为 1 的用户评论数量。