This is actually a useful bit of code if you want to do a little bit of manipulation on the index page of your wordpress blog. Rather than the ‘x comments, click here to see them’ link, you could choose to display the last one or two comments in excerpt format.
Here is how you do it:
<div class=”post-recent-comment”>
<?php
$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
$count = 1;
?><?php if ($comment_array) { ?>
<span class=”comment”><?php comments_number(‘No comment’,'1 comment’,'% comments’); ?></span> – Latest by:
<ul class=”commentlist”>
<?php foreach($comment_array as $comment){ ?>
<?php if ($count++ <= 2) { ?>
<li><?php comment_author_link(); ?><br /><?php comment_excerpt(); ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } else { ?>
<span class="comment">No comments so far</span>
<?php } ?>
</div>
Update: This piece of code has been fully implemented in a new theme I created, you can see the screenshots and read about it on the theme release page: Natural Green WordPress Theme. You can visit a working copy of this, in the wild so to speak, on my personal blog.
















Comments