Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Displaying Recent Comments (Site-wide)

Introduction

A common feature on blogs is to have the most recent comments on any post on the site displayed in the page's sidebar or footer. The code for Wolf's core comments plugin makes it possible to do this with some fairly simple code, which can then be manipulated in any way you like.

All comments1) on the site can be found with: Comment::find(). This returns various data about the comment as an array, so the easiest way to make use of it is to assign it to a variable:

$comments = Comment::find();

This “find()” also takes a variety of arguments, themselves in an array. This very simple example limits the number of comments returned to five:

$args = array('limit' => 5);
$comments = Comment::find($args);

The $comments variable can now be used in a foreach loop to display recent comments in whatever form you like.

Available elements

These are the various elements/pieces of information available to you from the Comment::find() method:

  • id = the unique ID of the particular comment
  • page_id = the ID of the page (article) which it is commenting on
  • body = the content of the comment
  • author_name = the name of the author
  • author_email = the author's email (required by the system)
  • author_link = the author's link (which is optional when making a comment)
  • ip = the IP number the comment came from
  • is_approved = “approved”/moderated comments have a value of “1”, those in the moderation queue have a value of “0”
  • created_on = date/time stamp for when the comment was created

There are some other functions that can also be used: see the comment plugin documentation for details.2)

Working example

Putting this together, here is the code for displaying the latest five approved comments with the author's name, the title of the article where the comment appears (linked), plus date:

<?php
// Get most recent [n] approved comments
$args = array(
    'limit' => 5, // set [n]umber here
    'where' => 'is_approved=1'
);
$comments = Comment::find($args); ?>
 
<ul>
<?php foreach ($comments as $comment) : ?>
<li><?php echo $comment->author_name; ?> on <?php echo Page::linkById(intval($comment->page_id)); ?>
 (<?php echo $comment->date(); ?>)</li>
<?php endforeach; ?>
</ul>

That code could be placed in a snippet, and the snippet called wherever you want the display of the five latest comments to appear.

Finishing Touches (options)

Displaying Gravatars

Wolf's comment plugin includes basic code for retrieving the Gravatar of the commenters if they have one. Use it like this:

<img src="<?php $comment->gravatar(40); ?>" />

The “40” is the square-size in pixels of the gravatar image. If you save a default “gravatar.png” image in your /public/images folder, it will display if the user does not have their own. You can grab this common default if you don't want to make one of your own:

Default gravatar.png 40x40

Linking directly to comment

Since it is possible to use an ID value as the anchor text for a link, the comment objects can be used to construct a unique ID value and so link directly to the comment itself, rather than simply the page it appears on. A good option is to combine the “page” and “comment” IDs like this: p##c##.

This requires modifying the comment-each snippet by adding an ID to the li. In the “foreach” find this line:

<li class="comment">

and change it to:

<li class="comment" id="<?php echo 'p'. $this->id .'c'. $comment->id; ?>">

That provides the anchor text for the link.

Use the following to form the URL to comment itself in the “recent comments” code/snippet:

<?php $anchor_id = 'p'.$comment->page_id.'c'.$comment->id; ?>
<a href="<?php echo Page::urlById(intval($comment->page_id)); ?>#<?php echo $anchor_id; ?>">Go to comment</a>
1) For a brief note on using the Comments plugin, see the Comments "how-to".
2) Or see the full phpDoc listings.
 
tutorials/recent_comments_list.txt · Last modified: 2012-03-14 12:01 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside