benkaminski.com


How to: Allow WordPress comments on specific pages only

Change or edit “post tags” appearance in WordPress


  benkaminski.com

April 4th, 2011

12 comments Leave A Comment


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:

Leftover visible div area from twentytwelve comments

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:
/wp-content/themes/twentyten/loop_page.php

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:


<?php comments_template( '', true ); ?>

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:

<?php if ( is_page( array( ID#, ID#, ID# ) ) )
 comments_template( '', true ); ?>

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:
MUCH better!  No more blank box where comments should be.
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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article currently has 12 comments.

Awesome! It works on me. Thank you so much..

Thanks for this tip ! It work perfectly

Hello,

It run works on my pages but do not works on my dropdown or subpages. Hope to hear from you. thank you

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?

Cat,
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

Hi Ben

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.

Thanks.

Cat

    A person nessaecrily help to make severely posts I would state. This is the first time I frequented your website page and to this point? I surprised with the research you made to make this particular post extraordinary. Wonderful process!

    I’d vetnrue that this article has saved me more time than any other.

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

-BEN

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.

Thanks, mate, awesome tip, worked like a charm.

hello,

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

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

Thank you

Related Topics: , , , , , , , , ,