Recently while working on a very secret project, we found a need to show comments on the homepage of the site. Normally, on single pages you can just call comments_template(); and it should display, but it wasn’t showing up. All we ended up getting was a blank div container. After doing a quick search online, we were able to find the answer. In this article, we will show you how to show comments on the homepage of your WordPress site.
Inside the loop right before endwhile, simply add the following code:
<?php $withcomments = "1"; comments_template(); // Get wp-comments.php template ?>
A single line $withcomments makes all the difference.
Note: this was a very simple one page site we threw up. So the theme elements were style.css, index.php, and comments.php file. We were calling a loop to display a specific page on the index.php file along with other things.
We hope that this would help some of you. Some of you may even have a better way of doing this. If you do, please share it with us in the comments.
working fine, but here shows 100s of comments how can i disable comments or else remove comments in code
You would remove the code if you no longer want to show the comments on your homepage
Hy,
Please where exactly am I adding the code? is it on my index.php? or front-page.php or where else?
I’m not good at coding but I can locate the files please
If your theme has a front-page.php you would add it to that file for your static home page
Thanks, it worked like a charm.
Glad our article could help
how to upload comments in word press website
If you mean you have a list of comments then you could use the plugin in our article here: https://www.wpbeginner.com/plugins/how-to-import-post-ideas-from-csv-spreadsheet-in-wordpress/
well done
I’m sorry if I sound dumb, but I’m not a PHP coder. Can someone please tell me which file I need to edit in order to place this text?
Thank you very much.
The functions.php file
Very useful! Thanks
I’m not sure exactly where to add this code. I have the comment box code at the end of my post, but where do I put this? Before or after?
Works great! Is there a way so when clicking post comment it won’t redirect to the posts page?
Has anyone had trouble getting this to work with Disqus?
That was excellent .. exactly i wanted it. Thanks thanks.
Thanks for the great tip (and generally fantastic resource), it set me in the right direction when I needed to create a modification for a child theme (using Genesis as the parent theme) I needed to use the following to get the comments to display:
global $withcomments;
$withcomments = 1;
comments_template( ' ', true );
I hope this helps someone else spend less time on this problem than I did
helped me! Thanks
Thanks. It helped me
helped me thanks!
thanks very much for your comments, you give me great hints about this problem. Although this post is published 3 years ago, but it still works.Thanks wpbeginner.
By the way, it’s a good idea to add this on the functions.php of child theme through hook, so your changes won’t lost when updating your theme.
Recently, when I was trying to solve this problem in a child theme I needed to add this line above the two shown above in order to get it work:
global $withcomments;
Thank you! The $withcomments = “1”; bit was exactly what I needed.
Use:
comments_template(“/comments.php”, true);
otherwise it just repeats the first posts comments for every post.
Yes!! Thanks for this additional tip, this was driving me crazy.
Great tip, thanks for posting about it too. I’ve noticed questions about this in the WordPress Support forums a few times with no answer given, so this will no doubt help quite a few people
Nice tip !
Thanks… let’s me try it
Just starting with wp so yes i need it thanx
Cool tip. At long last, I’m beginning to dig a little deeper into the WordPress code. I’ve subscribed to your weekly updates! dragonblogger mentioned your blog today and then I saw this tweet in my Gmail (I use @Rapportive)
Cheers,
Mitch