How to remove comment section from page only , not from posts pages?

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;
}

Leave a Comment