Skip to content

Instantly share code, notes, and snippets.

@CKMacLeod
Last active August 29, 2015 14:20
Show Gist options
  • Save CKMacLeod/eccc1e1e68071e84466a to your computer and use it in GitHub Desktop.
Save CKMacLeod/eccc1e1e68071e84466a to your computer and use it in GitHub Desktop.
Outputs a Page of Most Recent Site Comments Organized by Recency/Post
<?php
/*CK MacLeod, 2015.05.08, GPL
Function for displaying list of n most recent comments on a single page
in blog - consecutive comments from same post are grouped together
0.3 (2015.05.11): Added Series Length variable/logic
*/
//key settings, in plug-in version set in admin:
//total most recent comments:
$total_comments = 50;
//maximum size of comment before excerption, in words
$length_excerpt = 50;
//maximum number of consecutive comments before re-printing post title
$series_size = 4;
/*in this approach we do not utilize innate capacity of
get_comments() to order comments by post
*/
$comments = get_comments(array(
'status' => 'approve' ,//Change this to the type of comments to be displayed
'number' => $total_comments
));
//for implementation on new blog, would need logic for fewer than 50 comments
if (count($comments) < $total_comments) { $total_comments = count($comments); }
//initialize variables
$old_post = 0;
$number_comments = 0;
$series_comments = 0;
//open commentariat div
echo '<div class="commentariat">';
//produce comment list, keeping track of number of comments
foreach($comments as $comment) {
$current_post = $comment->comment_post_ID;
$number_comments++;
$series_comments++;
if ($old_post <> $current_post || $series_comments == $series_size ) {
/*if a new comment print linked title of new post
and if not first comment tie off previous post */
if ($number_comments > 1) { ?> </ul></div> <?php }
if ($number_comments <= $total_comments) { ?>
<div class="commentariat-post">
<a href="
<?php echo get_permalink( $comment->comment_post_ID ); ?>">
<?php echo get_the_title($comment->comment_post_ID);?></a>
<?php if ($number_comments <= $total_comments) { ?> <ul> <?php }
$series_comments = 0;
}
}//old post
/*produce comment - uniquely formatted for output to avoid overlap problems
with commenting systems - include in-reply-to links
*/
?>
<li class="comment" id="li-<?php echo $comment->comment_ID ?>">
<div class="commentariat-comment-head">
<div class="comment-author">
<?php echo get_avatar($comment->comment_author_email,32);?>
<?php echo get_comment_author_link($comment->comment_ID);?>
<span class="comment-datetime"><a href="
<?php echo get_comment_link($comment);?>"><?php echo $comment->comment_date;?></a></span>
</div>
</div>
<div class="comment-irt">
<?php if ($comment->comment_parent != '0') : ?>
<a href="<?php
$commentparent = get_comment($comment->comment_parent);
$commentparentpage = get_page_of_comment($commentparent);
echo htmlspecialchars(get_comment_link( $commentparent, array('page' => $commentparentpage) ) ); ?>
"> to
<?php echo $commentparent->comment_author ; ?>
</a>
<?php endif; ?>
</div>
<div class="comment-content" id="text-<?php echo $comment->comment_ID ?>">
<?php
$comment_text = $comment->comment_content;
$comment_text = strip_tags($comment_text);
$words = explode(' ', $comment_text, $length_excerpt + 1);
if (count($words) > $length_excerpt ) {
array_pop($words);
$comment_text = implode(' ', $words);
$comment_text = implode(' ', $words) . ' <a href="' .
get_comment_link($comment) . '">[. . .]</a>';
}
echo $comment_text; ?>
</div>
</li>
<?php
//close off list and div
if ($number_comments == $total_comments) { ?>
</ul></div>
<?php
}
//reset old_post variable
$old_post = $current_post;
} //end foreach
//close commentariat-wrapper and clear floats:
echo '</div><div style="clear:both; height:1px;">&nbsp;</div>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment