宅事

记录闲时弄的小东西

Habari中显示"最近评论"

Habari虽然有recentcomments和freshcomments这两个显示最近的评论的插件,但我试了下,运气差了些,没一个能用起来,所以只能自己动手了,看到miklb和我用一样的模板,他右边sidebar的Recent Comments做的真不错,和主题风格符合的好,就看着他网站的样子做了个.

在Habari的wiki上可以查到显示最近评论的方法,这儿先简单引用下:

1)在模板theme.php文件的add_template_vars这个函数中插入如下代码,修改"25"为你想显示的数目: 

  1. $this->assign('recent_comments',
  2.               Comments::get( array('limit'=>25, 'status'=>Comment::STATUS_APPROVED,
  3.                                    'type'=>Comment::COMMENT, 'orderby'=>'date DESC' ) ) );

2)在需要显示的地方(比如侧边栏sidebar.php)插入:

  1. <h3>Recent Comments</h3>
  2. <ul>
  3.   <?php foreach($recent_comments as $comment): ?>
  4.     <li>
  5.       <a href="<?php echo $comment->url ?>">
  6.         <?php echo $comment->name; ?>
  7.       </a> on
  8.       <a href="<?php echo $comment->post->permalink; ?>">
  9.         <?php echo $comment->post->title; ?>
  10.       </a>
  11.     </li>
  12.   <?php endforeach; ?>
  13. </ul>

这样就能实现最近评论功能了.

接着我写下现在使用的这个模板的"最近评论"代码,稍微美化了下,引入了Gravatar头像和RSS链接.

theme.php插入相同的代码,接着在CSS中加两段:

  1. div#recentcomments ul{
  2.         text-align:justify;
  3.         padding-bottom: 7px;
  4.         margin-bottom:7px;
  5.         border-bottom: #eaeaea 1px solid;
  6.     min-height: 25px;
  7. }
  1. div#recentcomments div.gravatar_mini
  2. {
  3.     float:left;
  4.         margin-right:5px;
  5. }

 接着在sidebar.php中加入这么一段:

  1. <div id="recentcomments">
  2. <h3><a href="http://zhais.com/atom/comments" title="Comments Feed"><img alt="comments Feed"
  3. src="<?php echo $this->theme_url; ?>/images/feed.png" /></a>&nbsp;Recent Comments</h3>
  4.     <?php foreach($recent_comments as $comment): ?>
  5.  
  6.    <ul>
  7.        <div class="gravatar_mini">      
  8.         <img alt="Gravatars Icon" src = "<?php echo 
  9.         "http://www.gravatar.com/avatar/".md5( $comment->email )."?s=25" ?>">
  10.        </div>
  11.    <div>
  12.    <a href="<?php echo $comment->url ?>">
  13.    <?php echo $comment->name; ?></a>
  14.    <?php echo " commented, &quot;".$comment->content."&quot;"." on the post "?>
  15.    <a href="<?php echo $comment->post->permalink; ?>">
  16.    <?php echo $comment->post->title."."; ?></a>
  17.   </div>
  18.   </ul>                                  
  19. <?php endforeach; ?>
  20. </div>

完成了 :)

  

0 Responses to Habari中显示"最近评论" Article comments Feed

Write a response

Play nice. Be constructive. Allowed HTML tags are: <a>, <strong>, <em> and <blockquote>

« 怎样在模板中… Habari中显示"… »