
Normally, we will put a recent comment widget on the sidebar of our blog. That widget will show all recent comments taken from all posts. But now, we want to do something different. We want to show the recent comment(s) for each of our post on the main page. Something like the Gizmodo (but of course they are not using WordPress). The recent comment(s) can be shown under each post, or next to it, depends on your theme and CSS styling. It will definitely look good on a grid theme.
Here is the way to implement it. Open your index.php file, and paste the following code inside the Loop, anywhere before the <?php endwhile; ?>. Preferably after <?php the_content(); ?>.
<!-- recent comment of each post -->
<div class="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 { ?> <!-- if there was no comment in that post,yet -->
<span class="comment">No comment so far</span>
<?php } ?>
</div>
<!-- end recent comment-->
Some explanation: this will fetch comment for each post, with the help of Post ID and will display latest 2 comments. You can change that count to any number you want. You just have to modify the line that says <?php if ($count++ <= 2) { ?>. Simple, right?
I am not providing any CSS examples for this one because you can do whatever you want with it. Put a light background color or image, style the heading, the <li> tag and so on. Make sure you use class rather than ID though.
There is no Live preview for this purpose but I have a screenshot for you. I am using my FTW2.o theme for this example.
Be creative with your CSS and post meta data arrangement. Good luck!
17 comments — Post a comment ↓
syuxx —
09 Aug 08 / #wahh, sekarang dah start experiment dgn php dah.
great.
jye —
09 Aug 08 / #gad, u’re the man!
creative2..
Aj —
10 Aug 08 / #nice idea.
Fath —
10 Aug 08 / #Cool ni flisterz.
Brandon —
10 Aug 08 / #Thanks, I’ve been wondering about this!
neutronics —
16 Aug 08 / #bro.. boleh bagi code tak untuk bezakan admin dengan user punya komen… tq
send kat my mail bole?
jbj —
16 Aug 08 / #Very good idea. Thanks for this usefull tutorial!
lut —
19 Aug 08 / #cooll!!
Meg —
11 Sep 08 / #So helpful.
Thanks!
minnesota care health insurance —
04 Oct 08 / #I really like the layout and colors that you chose for this website! It certainly is incredible!
win now —
04 Oct 08 / #I like your website ,and like to communicate with everyone on this issue!
rushmore casino —
05 Oct 08 / #I love this site, so thank you
Solidus_Snake —
09 Oct 08 / #I don’t quite understand why people waste their time configuring/modding/editing WP themes and config files while you can create your own blog engine with a bit more knowledge in PHP/JSP/ASP scripting.
Create ‘your own Wordpress’ and do whatever the hell you want with it. Trust me, it’s more fun that way. Haha.
fr7ty —
14 Oct 08 / #http://fr7ty.blogspot.com
I am specialized code for women and make everything new
The visit has added a lot of the code thing
Welcome
mycke —
03 Nov 08 / #this is very nice but i bumped into a problem, how do you modify the text colors or even the size of the comments?
screenshot: http://img396.imageshack.us/img396/1633/helplw4.jpg
please help. thanks very much.
Darold Bradley —
13 Nov 08 / #ap0kdhbtek56npc3
slot —
15 Nov 08 / #I enjoyed this site very much and have taken away a better insight. I will recommend this site to everyone I know. More people should step into .