testbox

commentsmainpage

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.

Code Implementation

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?

CSS styling

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.

Example

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.

View the screenshot.

Be creative with your CSS and post meta data arrangement. Good luck!

Share/save this: Delicious | StumbleUpon

55 comments

  1. wahh, sekarang dah start experiment dgn php dah.
    great. :)

  2. gad, u’re the man!

    creative2..

  3. nice idea.

  4. Cool ni flisterz. :)

  5. Thanks, I’ve been wondering about this!

  6. bro.. boleh bagi code tak untuk bezakan admin dengan user punya komen… tq

    send kat my mail bole?

  7. Very good idea. Thanks for this usefull tutorial!

  8. So helpful.

    Thanks!

  9. minnesota care health insurance - 04 Oct 2008

    I really like the layout and colors that you chose for this website! It certainly is incredible! :)

  10. I like your website ,and like to communicate with everyone on this issue!

  11. rushmore casino - 05 Oct 2008

    I love this site, so thank you

  12. Solidus_Snake - 09 Oct 2008

    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.

  13. 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

  14. 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.

  15. Darold Bradley - 13 Nov 2008

    ap0kdhbtek56npc3

  16. 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 .

  17. hello it is test. WinRAR provides the full RAR and ZIP file support, can decompress CAB, GZIP, ACE and other archive formats.
    pxvqtflpjgzzrlofwbdkxebfmnzwkhcaxpbhello

  18. Hey friends, Thank you !

  19. gambling guide - 13 Jan 2009

    This is a great page. And the contents are really that worth reading. I will add this to my own library

  20. Useful stuff, well done!
    More references in this would be greatly appreciated.
    jackson,
    createmlm.com

  21. 2]Get your own Nintendo Wii
    The games console with one of the strangest names on the planet (it’s pronounced ‘wee’) is also one of the most innovative. In fact, with its potential to change the face of the gaming landscape, Wii may be on the verge of a new era, if you’ll pardon the pun.
    The $400 Wii package comprises a square white console unit and stand that plugs into your TV or AV receiver, a wireless sensor bar that connects to the console and receives wireless commands from the battery-powered paddle-style Wiimote controller. Basic composite cables are supplied, but if you have a plasma or LCD the $50 component cable options will deliver better picture quality. The console also supports an SD slot, USB port and a DVD drive for games.
    Games data can be saved to SD memory cards, which weren’t officially released at the time of publication, but in lieu of their arrival, GameCube memory cards will suffice. The USB port can’t be used to save games either, but will enable future hardware upgrades such as a hard drive or DVD player, although no announcements have been made to this effect.
    Despite its DVD drive, the console is not a DVD player (a modification chip is required if you want to watch movies). Nor is it the high definition, hard drive-toting, networked multimedia online multiplayer gaming machine that is the Xbox 360 or forthcoming Playstation 3. Instead, it presents as the most affordable ‘next generation’ games machine available and, certainly, lacks for no important ingredients if you want to have fun.

  22. This is great, but it also shows the comments on my About Us and Contact Us pages, which I don’t want to happen. Can I put another IF statement in the routine to check for the page its on? What do I check for (in fact where can I find a list of the variables/functions available in WP to use)?

  23. Whew, I did a lot of searching and learned a lot. In the end, I was able to solve my own problem! To get the comments ONLY on the main page:
    Just after:
    I inserted this line:

    Then just after the last
    I inserted this line:

    Now it shows the comments only on my home page!

  24. Son of a gun! How was I to know I couldn’t put PHP code in a post!

  25. RaiulBaztepo - 29 Mar 2009

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  26. How can this be changes to show the whole comment – rather than just an excerpt?

  27. @Denise Young: You can replace the < ?php comment_excerpt(); ?> to < ?php comment_text(); ?>

  28. Thanks, I searched forever for this. I’m your newest fan.

  29. This is a great piece of code!!

    However, when the user clicks to add a new comment, or if the user goes to the specific post page, the comments are now shown twice because of the comments_template.php

    Is there a way to modify this code so that it only displays on the home page?

  30. I figured out on my own that if I add an if (is_home()) statement around the whole piece of code it works great!! Only displays this code on the homepage.

  31. This is exactly what I was looking for, and found a link to your site in the WP forums. Thank you!

  32. Great stuff; thanks very much.
    Now I’d like to make the excerpt a link back to the full list of comments for the post…

  33. ADettinoLicnof - 02 Jun 2009

    Cheap Amoxil Online
    This is especially important if you have any known allergies to other types of antibiotics.

  34. thanks nice blog

  35. I have put this script on my page, however it does not always show the latest comment when I refresh. Is there a reason for this?

    At first it seemed like I had to have a comment in each post for the new comments in the NEWEST post to show up.

    Now it just stopped showing recent comments for all my newest posts altogether, but it still shows the recent comments for the older ones…

    Why is this?

  36. Thanks for this information! I’ve been looking for this, and this was very helpful.

  37. thank you for your article, this is the good information

  38. I’m so glad I found this site…Keep up the good work

  39. Great site. Thanks

  40. Excellent tutorial dude…hits the spot!
    Just a note to others, remember that if you have a home.php then you need to make these changes in there as well!

  41. Hey, I found your blog in a new directory of blogs. I dont know how your blog came up, must have been a typo, anyway cool blog, I bookmarked you. :)

  42. Donnieboy - 12 Oct 2009

    Just wanted to drop you a line to say, I enjoy reading your site. I thought about starting a blog myself but don’t have the time.
    Oh well maybe one day…. :)

  43. We are a group of youths who run a non-profit, voluntary organization.

    Is there any forum where we may learn about php scripts so that we may use it for our website.

  44. Thanks for the blog post! We check often for tips and news…Keithhttp://www.eBuyHD.com

  45. Learn about their population. ,

  46. Although I am fully convinced of the truth of the views given in this volume, I by no means expect to convince experienced naturalists whose minds are stocked with a multitude of facts all viewed, during a long course of years, from a point of view directly opposite to mine. ,

  47. [... - http://www.flisterz.com is another good place of advice. Making money online is [… -

  48. thanks a lot, this post really helps!!!!

  49. Hi

    Can somebody explain how can I make visible post comment box in the homepage?

  50. Thanks for this, just what I was looking for.
    Jaya


Leave a comment