Complete and flexible blogging platform for ProcessWire

Find Recent Comments

The method findRecentComments() finds and returns a CommentArray of your Blog's most recent comments. It can be used independently or in conjunction with renderComments(), a method we have previously looked at.

<?php
 /**
 * Find a limited number of recent comments.
 *
 * @access public
 * @param int $limit Number of recent comments to find
 * @param int $start Where to start, like 0 (default: null = automatic, based on page number)
 * @param bool $admin Include non-approved and spam comments? (default: null = determine automatically)
 * @return CommentArray
 *
 */
 public function findRecentComments($limit = 3, $start = null, $admin = null)

This method accepts three arguments, $limit, $start and $admin.

The first argument $limit defaults to 3. It has to be an integer and will limit the number of recent comments to find. 

The second argument is $start. If specified, its value has to be an integer. It acts as an offset in the selector to specify from where to start fetching comments. The default is null.

The third argument $admin is boolean and determines whether non-approved and spam comments should be included in the results. The default is null, meaning, the method will automatically determine this. In such a case, if the page outputting the recent Comments is editable by the logged in user, non-approved and spam comments will also be returned. If set to false, it means not to include such comments irrespective if a user with the correct privileges is logged in or not. If set to true, it means to always show such comments.

The following code illustrates how to use the method findRecentComments(). Note that the method itself does not output any markup. Also note the code within the foreach loop. The properties $comment->cite, $comment->created etc are external to this method itself.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

$url = $config->urls->root . 'blog/comments/';
$out =  "<h4>Recent Comments</h4>";
$limit = 5;

//$comments = CommentArray (an object)
$comments = $blog->findRecentComments($limit, 0, false);

if(count($comments)) {

  $out .= "<ul class='links'>";
  
  //loop through the CommentArray
  foreach($comments as $comment) {
   $cite = htmlentities($comment->cite, ENT_QUOTES, "UTF-8");
   $date = $blog->formatDate($comment->created); 
   $out .= "<li><span class='date'>$date</span><br />
   <a href='{$comment->page->url}#comment{$comment->id}'>$cite &raquo; {$comment->page->title}</a>
   </li>";
  }

  $out .= "</ul>";
  $out .= "<p><a class='more' href='$url'>" . __('More') . "</a></p>";
} 

else {
  $out .= "<p>" . __('No comments yet') . "</p>";
}

echo $out;

The image below shows the unstyled output produced by the above code.

In the following example, we use findRecentComments() as the first argument of renderComments().

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

$limit = 5;

$start = ($input->pageNum-1) * $limit; 

echo $blog->renderComments($blog->findRecentComments($limit, $start), $limit);

Summary

We have learnt how to use the method findRecentComments($limit = 3, $start = null, $admin = null).The method does not output markup but instead returns a CommentArray which we can loop through and output several comment properties. The method can also be used together with the method renderComments().