宅事

记录闲时弄的小东西

Habari相关评论的插件,名字叫RN Related Posts,作者是天佑,下载和详细信息看这儿.

使用方法很简单,在需要显示的地方插入这行代码即可:

 <?php echo $related_posts; ?>

这儿写下适合我这个主题的用法,可以作为参考.

我的目标是在sidebar中显示"相关文章",并且只有在显示独立文章页面时sidebar中才出现"相关文章",排版格式符合整体界面.

1)上传related.png小图标到模板的images文件夹.

2)修改插件文件relatedposts.plugin.php的action_add_template_vars函数,改成如下形式:

  1. public function action_add_template_vars( $theme, $handler_vars )
  2. {
  3.     // Get the related posts only if config is setup properly and during
  4.     // 'display_post' action
  5.     if ( !empty( $this->config['num_post'] ) && Controller::get_action() == 'display_post' ) 
  6.        {
  7.         $theme->related_posts= $this->get_related_posts();
  8.         $theme->related_posts= "<h3><img alt=&quot;Related Posts&quot; src=".$theme->
  9. theme_url."/images/related.png />&nbsp;Related Posts</h3>".$theme->related_posts;                      
  10.        }
  11.     else {
  12.          $theme->related_posts= '';
  13.           }
  14. }

3)在CSS中加入:

  1. div#relatedposts ul{
  2.     style-type:none;
  3.     width:100%;
  4.     text-align:right;
  5. }
  6.  
  7. div#relatedposts a:hover {
  8.     border-bottom:2px solid #1ce0e7;
  9. }

4)在sidebar.php中加入:

  1. <!--Related Posts-->                                                           
  2. <div id="relatedposts">                        
  3. <?php echo $related_posts; ?>
  4. </div>

完成了.

 

Habari的wiki上可以找到3个标签云的插件:

HB-Cumulus:使用flash,标签组成一个立体的旋转圆球,下载和演示见这儿.此插件对中文不支持,但可修改flash源文件来支持,过程比较复杂,具体看这儿.

RN Tag Cloud 和 Vandango Tag Cloud 类似,后者是在前者的基础上修改得来的,具体看这儿这儿.

我这用了RNTagCloud,作了一些修改,因为RN标签显示用了<li>,会一竖列显示,所以先把tagcloud.plugin.php文件第400行左右的几个<li></li>给删了.

为了符合整体的布局,css中添加:

  1. div#tagcloud a:hover {
  2.     text-decoration:none;
  3.     border-bottom:2px solid #1ce0e7;
  4. }

上传tag.png小图标到模板目录的images文件夹里,修改sidebar.php,添加如下代码:

  1. <!--Tag Cloud-->
  2. <div id="tagcloud">
  3. <h3><img alt="Tag Cloud" src="<?php echo 
    $this->theme_url; ?>/images/tag.png" />&nbsp;Tag Cloud</h3>
  4. <?php $theme->tag_cloud( ); ?>
  5. </div>

就是这么简单,完成了.

接上面一篇文章,这篇讲怎样增加"最近发表的文章"这个功能.同样先在theme.php的add_template_vars函数中加入:

  1. $this->assign('recent_posts',
  2.        Posts::get( array('limit'=>5, 'status'=>'published',
  3.        'orderby'=>'pubdate DESC' ) ) );

修改"5"为你想要显示的数目.

接着编辑你想要增加这功能的地方,比如侧边栏 sidebar.php,加入下面的代码:

  1. <h2>Blog articles</h2>
  2.             <ul>
  3.                 <?php
  4.                     foreach ($theme->recent_posts as $post) {
  5.                         echo '<li><a href="', $post->permalink, '">',
  6.                         $post->title, '</a></li>';
  7.                     }
  8.                    
  9.                 ?>
  10.             </ul>

好了,完成了,此文资料来源Habari的官方wiki.

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>

完成了 :)