
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!
37 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 .
avodartus —
26 Dec 08 / #dutasteride reviews
affexyagernesiuts —
30 Dec 08 / #hello it is test. WinRAR provides the full RAR and ZIP file support, can decompress CAB, GZIP, ACE and other archive formats.
pxvqtflpjgzzrlofwbdkxebfmnzwkhcaxpbhello
win now —
09 Jan 09 / #Hey friends, Thank you !
gambling guide —
13 Jan 09 / #This is a great page. And the contents are really that worth reading. I will add this to my own library
jackson —
03 Feb 09 / #Useful stuff, well done!
More references in this would be greatly appreciated.
jackson,
createmlm.com
Mulkazarus —
14 Mar 09 / #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.
Terry —
21 Mar 09 / #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)?
Terry —
24 Mar 09 / #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!
Terry —
24 Mar 09 / #Son of a gun! How was I to know I couldn’t put PHP code in a post!
RaiulBaztepo —
29 Mar 09 / #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
gogo —
21 Apr 09 / #http://berdebah.mybrute.com
Denise Young —
27 Apr 09 / #How can this be changes to show the whole comment - rather than just an excerpt?
flisterz —
27 Apr 09 / #@Denise Young: You can replace the
< ?php comment_excerpt(); ?>to< ?php comment_text(); ?>Dustin Marson —
29 Apr 09 / #Thanks, I searched forever for this. I’m your newest fan.
mike —
05 May 09 / #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?
mike —
05 May 09 / #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.
Diane Faye Zerr —
07 May 09 / #This is exactly what I was looking for, and found a link to your site in the WP forums. Thank you!
Paul Williams —
22 May 09 / #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…
ADettinoLicnof —
02 Jun 09 / #Cheap Amoxil Online
This is especially important if you have any known allergies to other types of antibiotics.
dizi izle —
22 Jun 09 / #thanks nice blog