How to remove comment section from page only , not from posts pages?
3 Answers
If you want to display comments and the comment form on posts but not on pages, you need to split up the logic in your template file to call comments_template()
depending on the type of the item displayed (post or page). There are two ways to do this: either you keep one template file for both items, and use conditional tags:
if (!is_page()) {
comments_template();
}
The other option is to use both a single.php
template file for your posts and a page.php
for your pages (see the Template Hierarchy for more information). Just leave out the call to comments_template()
in the page template. If there are no other differences between a post and a page layout, one combined template file with conditional tags is probably better for maintainability.
If you want to do this "from a distance", so where the template file already includes a call to comments_template()
, you can create a plugin that hooks into the comments_template
filter and redirects it to an empty file in the directory (well, it could even be the plugin file itself - since it only contains PHP code, it won't display anything - but this will be confusing to others).
add_filter('comments_template', 'no_comments_on_page');
function no_comments_on_page( $file )
{
if ( is_page() ) {
$file = dirname( __FILE__ ) . '/empty-file.php';
}
return $file;
}
-
Hi Jan, Thanks for the answer. Is there any plugin to do this without going into code? Nov 15, 2010 at 17:33
-
@metal-gear-solid: I updated my answer with an example to do this from a plugin. I don't know whether a plugin exists that does this, but it's really easy so you can place it in a plugin file yourself. Nov 15, 2010 at 18:08
-
Manually I can remove comment section for each page to uncheck these options farm2.static.flickr.com/1280/5179502796_b9e09c10ba.jpg. And i just found this plugin which will keep these checkboxes unchecked for all pages, but not for posts wordpress.org/extend/plugins/page-comments-off-please Nov 15, 2010 at 18:59
I like a lot this snippet from Hybrid theme at start of comments.php
template that will flexibly disable comments if they are not supported or disabled on per-post/per-page basis:
if ( !post_type_supports( get_post_type(), 'comments' ) || ( !have_comments() && !comments_open() && !pings_open() ) )
return;
depending on the theme.
check pages.php code and remove stuff related to comments
be careful not to break the design.
If you can post the code of pages.php here, hope we can help tell u modifications that need to be done