
In WordPress, comment template is normally displayed in a single or page view. However, there might be some specific situation where you want to display the comments on your main page, right underneath your entry, before the next entries.
By default, you will be able to find the comment template tag - <?php comments_template(); ?> inside a single.php and/or page.php. Trying to put that template tag inside your index.php will not going to achieve anything.
Actually, you can. I was looking inside WP’s core file, specifically comment-template.php inside the wp-include folder. This is the fragment of code that we will discuss here.

If you are comfortable with editing the core file, you can just add is_home() together with that is_single() and is_page(). But there is an easier way. Notice the $withcomments variable there? We are going to use it.
Consider we are using the Default WP theme, open your index.php file, and add this line of code
<?php $withcomments = 1 ?>
right after <?php get_header(); ?>
Then add the comment template tag <?php comments_template(); ?> inside the Loop before <?php endwhile; ?>
So the entire index.php file will be like this
<?php get_header(); ?>
<?php $withcomments = 1 ?>
<div id="content" class="narrowcolumn">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
<?php comments_template(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
It depends on your theme though. But the foundation is basically the same.
Since the code for comment form is also inside the comment template, so you will also see the comment box at the end of each entry. And when you post a comment using that comment form, the comment will still be posted but you will be directed to the single view.
You can see a (very long) screenshot here.
Well, I think there is a way to remove the comment form, and put in in another file that will only be displayed inside the single/page view. Or anything. So, yeah, try it yourself
Good luck.
20 comments — Post a comment ↓
Bat —
02 Jun 08 / #I see..This is how to make a site that similar to A Brief Message. Although they didn’t use wordpress(I think).
So, Flisterz, maybe you can make one for our local scene.
And, thanks for the article. Really helps a lot for a future reference.
Artgeex —
02 Jun 08 / #thanks for the info. Aku pun dah lama tak baca pasal benda terkini untuk WP. Still using the old tech of WP
btw, aku nak request boleh? Kalau ko dah buat, ko bagi aku link nya ok. Kalau belum, ni requestnya: buat tutorial pasal nak guna image dalam custom field dan appear macam frontpage ko ni. Boleh? Alaaa bolehla (sambil buat muka comel ekekek)
ikram-zidane —
02 Jun 08 / #good tutorial.
actually artgeex, he’s using the one written on derek’s blog..
http://5thirtyone.com/archives/875
custom field is becoming more famous everyday, there’s a lot of tutorials for it.. just google…
flisterz —
02 Jun 08 / #@Bat : Haha yeah I dont think they use WP.
Your welcome Bat
@Artgeex : Rasenye dah lame benda ni ade, ramai x tau je. heh.
@Ikram @Artgeex :
I never read that tutorial before actually, but yeah, it is basically the same thing. A lot of ppl are writing about custom field right. Its a great thing to play with
Artgeex —
02 Jun 08 / #@ikram_zidane: I know I can Google nearly everything, but I visit Flisterz’s blog nearly everyday. That’s why I made the request. Manalah tau, Flisterz got a more easy understanding way ke, kan?
ikram-zidane —
02 Jun 08 / #Lol.. sorry dude.. best assumption/guss based on the look..
okay. there are plenty on the web and the simplest i found was on webdesignerwall.. check it out.. on the sidenote, i beg to differ, i think the understanding is the same all the way.. its the executions that are different.. i like this one http://justintadlock.com/archives/2007/10/24/using-wordpress-custom-fields-introduction and the one on WDW the best..
Artgeex —
03 Jun 08 / #i dont agree with you with the understanding stuff
i simply will walk away with the previous example that you gave me. The codes looks cluttered for someone that cant do programming or a noob, like me. I just hate the font that he used to indicate that it is a bunch of codes.
and I think the new one is more comprehensible.
thanks btw
Cristian Eslava —
08 Jun 08 / #Hi,
First congratulations for your work, great blog.
You can see a very customize template of Wordpress here:
http://justtellmewhy.com
Best regards
dizi izle —
11 Jun 08 / #thanks
Tom —
14 Jun 08 / #Thank you from a complete newbie. It worked perfectly.
Tom
Bransby —
28 Jun 08 / #This has been exceedingly helpful for me, thank you very much. I’m a total noob to php and wordpress, but am RTFM and getting there slowly.
Quick question though, you mention at the end that “And when you post a comment using that comment form, the comment will still be posted but you will be directed to the single view.” How can I stop this from happening? How do I make a comments submission redirect back to the main page? For my purposes the single page view is pretty useless, and I’d like to keep it all on the main page. TIA
B
Bransby —
28 Jun 08 / #Oh, and, I stumbled you, hope that’s ok.
flisterz —
28 Jun 08 / #@Bransby: I’m sorry, I haven’t found the solution for that.
Shaun —
30 Jun 08 / #<!– comments …………………………… –>
<?php $wp_query->is_single = true; ?>
<?php comments_template(); ?>
<!– /comments –>
Dude add this after your post in index.php - so much easier
Jon —
16 Jul 08 / #I’m trying to use this in a current Wordpress install - but it seems this functionality has now been removed from WP2.6 - is anyone else finding this problem?
Bart Lee —
28 Jul 08 / #I also found it didn’t work in 2.6, but editing comment-template.php as described did. Thank you!
Amanda Wray —
29 Jul 08 / #This has been a very valuable post for me in solving the issue of having comments show up on a post page. Worked very well, except that it won’t let me put the comments template inside the loop so I can have it show up after each post.
It give me this error:
Fatal error: Cannot redeclare k2_comment_type_detection() (previously declared in /home/.ceno/wrayco/www.sgvhabitat.org/wp-content/themes/cutline-3-column-split-11/comments.php:15) in /home/.ceno/wrayco/www.sgvhabitat.org/wp-content/themes/cutline-3-column-split-11/comments.php on line 23
Any thoughts?
Ed Atrero —
01 Aug 08 / #Cool… works great! Thanks Flisterz.
jon —
10 Aug 08 / #just wanna check something
jon —
10 Aug 08 / #Hey great tutorial, I just wanted to see if i would be redirected to your main pageafter posting the comment, I just figured out how to put wordpress on my website, but now I want to do what you’ve just demonstrated in your turorial AND redirect to the main page instead of the single page thing wordpress has set up for posted comments - any suggestions anyone? Thanks