Getting & Giving Support
- Ref. - PHPDoc
Wolf CMS books section
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);
$comments variable can now be used in a
foreach loop to display recent comments in whatever form you like.
These are the various elements/pieces of information available to you from the
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
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.
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:
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:
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>