How to: Allow WordPress comments on specific pages only
So, everyone knows that you can easily turn on and off comments on WordPress pages and posts. Unfortunately for most (depending on theme) when you do this, it leaves a little unsightly blank area or “Comments are Closed” text behind using the default “built-in” method.
This gets annoying as it leaves behind traces of itself and doesn’t look very professional.
Example 1: Leftover junk from using the built-in “comments off” feature:
What we’re going to do now is conditionally ALLOW comments to exist only on certain PAGES (not posts) by ID number. This way, not only is the comment table gone, but any traces it usually leaves of itself will also disappear due to this method. Where you find the code may depend on your theme. I am using the TwentyTen theme for this example.
In the TwentyTen theme, the PHP code snippet is located in the following location:
Depending on theme, you may need to just open: /wp-content/themes/themename/page.php
Either way, you’re looking for this line of code:
This is the page that calls to the “comments-template” which is the entire structure of the comments table and all of it’s contents.
Now, once you have located this snippet, the rest is very easy. Lets say that you want to ALLOW comments on only a few PAGES and have them completely stripped from others. Here’s how:Replace snippet with this line of code:
Utilizing the PHP “if” statement and “is_page” (WordPress) and then adding the array of pages allowed you can make good use of this tutorial. Just replace the “ID#” with the page ID that you would like to ALLOW comments on. You can easily locate your page ID# by hovering over the link. You can even use perma-link structure in lieu of ID#’s. For example replace ID# with “my-home-page” or whatever title your page shows in the URL bar of your browser.
Example 2: How it looks after code has been applied:
See… no more unsightly blank area where the comments should be.
Hope this tutorial helps you in further customizing your WordPress themes. Please let me know if you have any questions or if this worked for you.
I have showing a list of members on word press site in a page. when I click on the page it shows all the members and also a search. now when I click on any user in that list it shoes author=1 same like other author=2, author=3. Now I have integrated a comment template in this users. when I comments on this profile the page redirect to the post from that user.
can I show each users as page so as the comments will work for that particular user. means when the author is click from the list it show automatically as page and this should work for all the users, can this be happen. I need some urgent help on this
Is there any way to add a comment section in author.php page which having users profiles. this should allow other users to comments on user profiles.
please help to solve this issue.
I’ve tried replacing the code and my page ID is 163. It didn’t do anything. I use the Graphene theme. I found exact line of code. I use apatana studios to manipulate my php.
@ Sunny, if I can see your code or a snippet where you have applied it perhaps I can help. Being that this code is definitely theme dependent, it could vary due to your theme. Post your code and I’ll give it a look.
Nice and simple code thanks.
I would like to use this on a site I am developing using the evolve theme – there are a few pages that fall under the WP e-commerce plugin – namely a product page and so on that show the comments no matter what I do.
I am using a static page for my home page and a separate page for the blog.
When I check the index.php file for the theme there are 4 areas where I see the comments_template function being called – do I update this in each instance?
Also – do I include the blog page ID in the list of “allowed” pages so that comments still work on the posts or is that not necessary.
And lastly is the array using just the page ID number (i.e. 80) or with the “ID” part (namely ID80). Sorry if that sounds stupid.
As far as comments showing on the shopping cart plugin, that is a function that is built into the cart itself. It may or may not be using the native comments system built into WordPress. You would either have to go into the actual plugin folder find and edit the call to the comments template and run the risk of losing your work every time the plugin has to be updated — this is obviously not recommended. This should be taken care of in your THEME’s functions.php page, but thats an entirely different blog post.
With regard to your index.php file, I can’t imagine 4 separate calls to the comments template unless there are 4 separate comments areas, but i’m sure that’s possible. I would like to see this pages code before I can come up with an answer. Feel free to use the contact form to write me with the code.
As far as the syntax goes for the list of allowed pages it should look like this:
< ?php if ( is_page( array( 148,57,8 ) ) ) comments_template( '', true ); ?>
There is no need for the “#” or the phrase “ID”. Please contact me with your code so I can help with the multiple instances of the comments template.
ben I want to only add comments on page 28. I put this code in
And it actually shows up on the page. Not working at all. Can you help me?
It run works on my pages but do not works on my dropdown or subpages. Hope to hear from you. thank you
Thanks for this tip ! It work perfectly