建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

wp_list_comments()评论展示函数的使用方法(WordPress 实现文章评论排行榜)

GG网络技术分享 2025-03-18 16:14 0


wp_list_comments()评论展示函数的使用方法

wp_list_comments()是用于读取wordpress文章或者页面评论数据的函数,把WordPress的评论功能进行了模块化,wp_list_comments函数在主题中配合comments_template使用更佳。

用法

参数

参数说明

1、$walker 自定义样式类名

2、$avatar_size (可选) 头像大小 默认: 32

3、$style (可选)评论样式控制,你可以添加 ‘div’, ‘ol’, 或者’ul’, 来展示你的评论,如:

默认值是’ul’

4、$type (可选) 评论展现方式,参数可以是 ‘all’、’comment’、’trackback’、’pingback’、’pings’. ‘pings’ 包括’trackback’ 和 ‘pingback’.

默认值: ‘all’

5、$reply_text (可选) 评论的回复链接,(可以通过函数get_comment_reply_link function 获取)

默认: ‘Reply’

6、$login_text (可选)用户必须登录后评论的提示信息,默认: ‘Log in to Reply’

7、$callback (可选) 回调函数,通过回调函数来自定义你的评论展示方式。Default: null

8、$end-callback (可选) 关闭评论后调用的自定义函数,Default: null

9、$reverse_top_level (可选)评论数据是否倒序显示,Default: null

10、$reverse_children (可选) 子评论数据是否倒序显示,Default:null

自定义评论列表

我们可以用wp_list_comments()参数中的callback实现自定义评论列表,这样可以按照自己想要的样子设计评论。

WordPress 实现文章评论排行榜

用到了WordPress功能函数Query_post()的一种高级用法,就是获取本周或当月或最近30天评论最多的一定数量的日志。

使用方法是将以下各段代码放置到需要显示最热日志的主题模板文件中适当的位置即可,如边栏(sidebar.php)。

所有时间内评论最多日志

<ul>  <?php query_posts(\'post_type=post&posts_per_page=10&orderby=comment_count&order=DESC\'); while (have_posts()): the_post(); ?>
<li><a href=\"<?php the_permalink(); ?>\" title=\"<?php printf(esc_attr(\'Permalink to %s\'), the_title_attribute(\'echo=0\')); ?>\" rel=\"bookmark\"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

这段代码默认显示前10篇评论最多的日志,数量10可修改为其它数值。

本周评论最多日志

要显示本周评论最多日志,我们就可以使用如下的代码,也就是在前面代码的基础上再添加一些额外的参数来实现:

<ul> <?php $week = date(\'W\'); $year = date(\'Y\'); query_posts(\'post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=\' . $year . \'&w=\' . $week);  while (have_posts()): the_post(); ?>
<li><a href=\"<?php the_permalink(); ?>\" title=\"<?php printf(esc_attr(\'Permalink to %s\'), the_title_attribute(\'echo=0\')); ?>\" rel=\"bookmark\"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

最近30天评论最多日志

<ul> <?php function filter_where($where = \'\') {     //posts in the last 30 days     $where .= \" AND post_date > \'\" . date(\'Y-m-d\', strtotime(\'-30 days\')) . \"\'\";     return $where; } add_filter(\'posts_where\', \'filter_where\');  query_posts(\'post_type=post&posts_per_page=10&orderby=comment_count&order=DESC\'); while (have_posts()): the_post(); ?>
<li><a href=\"<?php the_permalink(); ?>\" title=\"<?php printf(esc_attr(\'Permalink to %s\'), the_title_attribute(\'echo=0\')); ?>\" rel=\"bookmark\"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

“30 days”可以根据需要修改为其他值(如“1 year”, “7 days”, 等)。

本月评论最多日志

类似地,显示当月评论最多的日志,可以使用下面的代码:

<ul> <?php $month = date(\'m\'); $year = date(\'Y\'); query_posts(\'post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=\' . $year . \'&monthnum=\' . $month); while (have_posts()): the_post(); ?>
<li><a href=\"<?php the_permalink(); ?>\" title=\"<?php printf(esc_attr(\'Permalink to %s\'), the_title_attribute(\'echo=0\')); ?>\" rel=\"bookmark\"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>
</ul>

WordPress 实现文章评论排行榜 (https://www.wpmee.com/) WordPress使用教程 第1张

标签:

提交需求或反馈

Demand feedback