testbox

display.jpg
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.

Code

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.

…but, you need to know

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.

Share/save this: Delicious | StumbleUpon

111 comments

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

  2. thanks for the info. Aku pun dah lama tak baca pasal benda terkini untuk WP. Still using the old tech of WP :P

    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)

  3. 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…

  4. flisterz - 02 Jun 2008

    @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 :)

  5. @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?

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

    Lol.. sorry dude.. best assumption/guss based on the look..

    @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?

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

  7. 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 :P

  8. Hi,
    First congratulations for your work, great blog.
    You can see a very customize template of Wordpress here:

    http://justtellmewhy.com

    Best regards

  9. Thank you from a complete newbie. It worked perfectly.

    Tom

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

  11. Oh, and, I stumbled you, hope that’s ok.

  12. flisterz - 28 Jun 2008

    @Bransby: I’m sorry, I haven’t found the solution for that. :(

  13. <!– comments …………………………… –>
    <?php $wp_query->is_single = true; ?>
    <?php comments_template(); ?>
    <!– /comments –>

    Dude add this after your post in index.php – so much easier

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

  15. Bart Lee - 28 Jul 2008

    I also found it didn’t work in 2.6, but editing comment-template.php as described did. Thank you!

  16. Amanda Wray - 29 Jul 2008

    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?

  17. Cool… works great! Thanks Flisterz.

  18. just wanna check something

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

  20. I have looked at the script.,

  21. Bog thanks for this mega informative website :) )

  22. Wow. Nice site. I like your design. Very, very thanks!

  23. Good design and cool themes on this website. Thanks for autor(s) ;)

  24. Very good website! I add this site to bookmark. Thanks ;)

  25. Very good website and very good themes. Thank you master!!! Regards ;)

  26. Real good website. Thank you master!

  27. Good website. I like the all pages and all comments. Thanks for all!!! Regards!!!

  28. Nice website. Very cool content. Thank you!!!

  29. Alien Among Us - 29 Dec 2008

    Awesome dude. I was looking all over for this. Found you from this google search “SHOW COMMENT FORMS ON WORDPRESS MAIN INDEX”

  30. is there a plugin that does this?

  31. flisterz - 31 Dec 2008

    @Pranav Shah:
    I am not sure about that.

  32. Good website, cool content and design. Big thanks for webmaster. :)

  33. Hi. Thanks for showing me how to display comments immediately after a post. Works great, except, do you have any idea why my right sidebar disappears when the comments show?

    I also submitted the question to the WP forum, but perhaps you might have the quick answer. Meanwhile, I had to revert until I get an answer.

    I use Cutline 3-column, WP 2.6.3. Thanks Bro.

  34. Bog thanks for this mega informative website :) )

  35. Hi owner! Cool site. Cool design. Cool comments :) )

  36. Good website with wery informative pages. I like it and add this site to bookmarks.

  37. Good thematic website and great design. Big thanks for webmaster!!! ;)

  38. Nice…. Cool work and excellent Site! Good source of factual information! Cheers.

  39. Very good and informative website. Thank you webmaster!

  40. Nice site! I real happy and impressed with this site!

  41. exciting and communicative, but would make something more on this topic?

  42. Sweet! Thank you so much! It worked beautifully! You are awesome!

  43. Thanks very much for posting this. I was Googling forever on how to do this.

  44. Cool site. I shall visit again and I shall inform my best friends. Thank you!

  45. Good website and beautiful design!!!!!!!!! Regards!!!!

  46. Good website and nice content!!! Thank you, master!

  47. Very good website. Thank you master!!!!!!!!!!!!!!!!!!!!!!!!!! ;)

  48. Cool design and very informative site. Thank you mister!

  49. Hi owner! Cool site. Cool design. Cool comments :) )

  50. Good website and nice content!!! Thank you master!

  51. Good website…. Happy comments ;) …. I can post my link here?

  52. how can i put image on the side of my comments ?

  53. Good website and happy messages. I like it. Thank you master!

  54. Cool site. I like it. Thank you!

  55. Very informative website. Thank you! +1 bookmark !!!

  56. Good site!!! Thank you!

  57. Bog thanks for this mega informative website :) )

  58. Good website. I like the all pages and all comments. Thanks for all!!! Regards!!!

  59. Good website and beautiful design!!!!!!!!! Regards!!!!

  60. Cool design and very informative site. Thank you mister!

  61. Thank you for the codes about showing comments on post page; worked perfectly.

    Art

  62. Very good website. Thank you webmaster!!! Best regards!!!

  63. Good website. I shall visit again and I shall inform my friends ;)

  64. Very good website! Thank you!

  65. Nice site! I like info from this site. Thank you!

  66. Hi all. Goog site and cool design. Big respect for webmaster that make this site!

  67. Wow. Nice site. I like your design. Very, very thanks!

  68. Good website and very nice comments. Thank you!

  69. Wow. Nice site. I like your design. Very, very thanks!

  70. Real nice comments. I want do also. Sorry for comment. I need moneys ;) Regards!!!!!

  71. Very interesterd website, I very like all articles and all nnice comments ;) Thanks

  72. Cool site. I shall visit again and I shall inform my best friends. Thank you!

  73. Cool website! Best regards and nice day!

  74. Good website and very nice comments. Thank you!

  75. Cool comments. Big thanks for all visitors and for author. I love this site!!!

  76. Im very like doing this research paper on legalizing pot.
    I like information that you

  77. Jaydaloren - 11 Apr 2009

    It’s nice ! Information will help all ! thank you …

  78. Very good website. Thank you master for this all pages!!! Regards!

  79. Very good site! I like it! I just wanted to pass on a note to let you know what a great

  80. Nice website. Very cool content. Thank you!!!

  81. Good work! Excellent Site. Good source of factual information! Cheers.

  82. Very good website. Thank you master!!!!!!!!!!!!!!!!!!!!!!!!!! ;)

  83. Hi webmaster! Very good website and very intereted pages!! I love this resource!!! Thank

  84. Good thematic website and great design. Big thanks for webmaster!!! ;)

  85. Very good site! I like it! I just wanted to pass on a note to let you know what a great

  86. Nice site! I real happy and impressed with this site!

  87. excellent article, thank you so much; this is exactly what I was looking for !!

  88. ty my admin niceee..

  89. Felix Karlsson - 23 Jul 2009

    Thanks a lot! Has someone figured out how to not involve single.php in any way. Redirect to index.php after submiting a comment.
    Need a solution fast, please help!

    Please contact me at felixkarlsson91@gmail.com

  90. @Felix and others, this worked for me to redirect commenters to the home page. Users on single post pages are not redirected.

    Add a hidden input to the comment form, name it “redirect_to”, and assign a value of the current URL.

    It should look something like this, hope the formatting doesn’t get too mangled:

    <input type=”hidden” name=”redirect_to” value=”" />

    Hope this helps! I was sure glad to discover it buried in my archives.

  91. Whoops, the value was a php string. That’s not going to show up.
    Basically, you just need the current url. My theme is using $_SERVER['REQUEST_URI'], and I’m sure it’s for some good reason that I can’t recall.

  92. Very nice, thanks a million.

  93. awesome post. however did anyone try that with WPMU as well. i actually don’t need to get the comments shown on my index, i just have a sidebar that shows my global recent comments. i built that sidebar myself and it works almost fine. however i can’t seem to get the comment_date and time to work in the sidebar on my index-page. if i visit a post on my mainblog the date and time of my comments will show up in the sidebar, so i thought maybe it’s just a problem like you described in your post. maybe you just need to enable the index for getting the right comment-information. however if i try to change the core files with the is_home() it won’t work and if i try to add the $withcomments=1; on the index page, the date isn’t showing as well. any ideas?

  94. thanx for sharing this.

    i have a quick question though, when i use this hack i don’t get comments pagination.

  95. Very the curious guestbook design. What CMS do you use ?

  96. Hi! Depressing klooper aside as my english jer, buti vertical charming re respond .

  97. Very beautiful gb style of site. What CMS do you use ?

  98. great info!

  99. There’s an easy way to do this using an second comments template that you can customize. Discussion can be found here Wordpress Comments Hack

  100. Chameera - 08 Dec 2009

    Thanks a lot! i hv been looking for this for a long time… thank you again… just wt i need!

  101. Very nice site!

  102. I am definitely bookmarking this page and sharing it with my friends.

    :)

  103. Thanks Flisterz,

    It was so simple I almost didn’t get it!

    Finally figured out that I needed to edit the Theme’s index php file!

    Thanks again.

    Sasha

  104. Flisterz Thanks very much!!! I was looking everywhere for this solution, tried many others and failed. This worked like charm all Thanks to YOU!!!

  105. Worked perfect, thanks heaps.

  106. Awesome tuto mate. I was looking around for that kind of info so thanks for the help :)

  107. It helps me! Thanks!

  108. Не дурный сайтег и гостевуха хорошая, так что респект автору!

  109. Весьма интересный сайт, спасибо!

  110. This may be a quality site.

    I have bookmarked this particular site and also I will notify my friend about it.

    Thankfulness


Leave a comment