Recent Comments

From Habari Project

Jump to: navigation, search

It's really simple to get a listing of recent comments to output in your Habari theme. This page describes how to manually add recent comments support by editing your theme. If you don't want to write code, there is a Recent Comments plugin available from the Habari Extras repository that lets you customize how many comments to display and how to display them.

There are just two steps to add recent comments to your blog.

First, you need to make an array of recent comments available in your theme template. Add this code to the theme.php file for your theme inside the add_template_vars function:

               Comments::get( array('limit'=>25, 'status'=>Comment::STATUS_APPROVED, 'orderby'=>'date DESC' ) ) );

If you are using the Pingback plugin, and want to remove those from your recent comments, you can add the type parameter when retrieving comments, like this:

              Comments::get( array('limit'=>25, 'status'=>Comment::STATUS_APPROVED, 
                                   'type'=>Comment::COMMENT, 'orderby'=>'date DESC' ) ) );

This tells Habari to store the 25 most recent approved comments into a variable called $recent_comments that will be accessible in your template. You could separately list most recent pingbacks with a modification of the code.

Second, add this to your theme's template where you want the comment listing to appear:

<h3>Recent Comments</h3>
  <?php foreach($recent_comments as $comment): ?>
      <a href="<?php echo $comment->url ?>">
        <?php echo $comment->name; ?>
      </a> on 
      <a href="<?php echo $comment->post->permalink; ?>">
        <?php echo $comment->post->title; ?>
  <?php endforeach; ?>

This loops through each of the comment objects in the array of recent comments using a foreach loop and prints them. You can edit the HTML to change how the comments are displayed.

  • $comment->name is the name of the commenter.
  • $comment->url is the URL of the commenter, such as their web site.
  • $comment->post is the post object on which the commenter commented.
  • $comment->post->title is the title of the post to which the comment applies.

You could even use $comment->content to output the content of the comment, if you wanted.

Likewise, if you'd only like to show an excerpt of the comment, you could add to your theme.php (under public function action_init_theme()

Format::apply( 'summarize', 'comment_content_excerpt', 30, 1);

where 30 is the maximum number of words to display, and 1 is the maximum number of paragraphs. In this instance, you would call $comment->content_excerpt where you wanted your truncated comment.

Personal tools