之前写过《WordPress 创建显示所有评论的页面》,能显示近一年来的所有评论。现在简单改一下代码,实现显示近期的 n 条评论。1、2、4 条步骤同前文。第 3 步代码改为:
<?php //WordPress 创建显示近 10 条评论的页面 $comments = get_comments('$status=approve'); $month = date('Y-m'); $shortmonth = date('m'); if ($shortmonth == 1){ $lastmonth = 12; $lastyear = date('Y') -1; $start = $lastyear.'-'.$lastmonth; } else{ $lastmonth = $shortmonth - 1; $start = date('Y').'-'.$lastmonth; } foreach($comments as $comm) { $comments_month = date('Y-m', strtotime($comm->comment_date)); $comments_year = date('Y', strtotime($comm->comment_date)); //未来实现根据评论数量自动翻页 // if ($count >=5) // posts_nav_link(); //判断近期的几条评论 if ($count >= 10) break; if ($comments_month < $start) break; if ($comments_month != $month) echo '<h1 class="entry-content"><b>' . $comments_month . '</b></h1><p>'; $link= esc_url( get_comment_link($comm->comment_ID) ); $title=get_the_title($comm->comment_post_ID); //以下 $url 用来判断作者 url 是否为空,如为空则下划线 $url = $comm->comment_author_url; if ( !empty($url)) $urltxt = '<a href="'. $url .'" rel="nofollow">' . $comm->comment_author . '</a>'; else $urltxt = '<span style="border-bottom: 1px dashed #555;">' . $comm->comment_author . '</span>'; echo '<li style="word-wrap:break-word;overflow:hidden;margin: 0 20px 0 25px;text-align:left;line-height:170%;text-indent:0;">'. $urltxt . '在<a href="'.$link.'">'.$title .'</a> 上的评论:' . $comm->comment_content . ' (' . $comm->comment_date . ')</li> '; $count += 1; $month = date('Y-m', strtotime($comm->comment_date)); } ?>
效果见:评说。
@Betty 好吧,感谢支持……
貌似这 demo 看不出效果啊。
@大肥羊 看不出效果吗?只显示 10 条啊。
@Yang 刚才看是一条都没有啊。
@大肥羊 可能一时抽风吧…… 😳