/ 软件 / 1063浏览

WordPress创建显示近期评论的页面

之前写过《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 . '&nbsp;&nbsp;&nbsp;(' . $comm->comment_date . ')</li> ';

	$count += 1;

	$month = date('Y-m', strtotime($comm->comment_date));
}

?>

效果见:评说

6

  1. Betty

    :mrgreen:

    Google Chrome 39 · Mac OS X 10.10
    1. S

      @Betty好吧,感谢支持……

      Opera beta 26 · GNU/Linux
  2. 大肥羊

    貌似这 demo 看不出效果啊。

    Firefox 34 · Windows 7
    1. S

      @大肥羊看不出效果吗?只显示10条啊。

      Chromium 39 · Ubuntu
      1. 大肥羊

        @S刚才看是一条都没有啊。

        Firefox 34 · Windows 7
        1. S

          @大肥羊可能一时抽风吧…… 😳

          Opera Mini 7 · Android

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注