admin管理员组

文章数量:1202597

When I consult Google about how to remove or hide comments on certain pages, the answers seem to be "all or none." That is, there are plenty of explanations as to how to disable further comments on the page, but nothing on how to make the existing comments not show at all. There is plenty of advice on how to disable comments site-wide, but of course, that's not a serious solution. Suggesting that I delete the comments isn't a solution, because those comments are welcome on other pages.

For instance, our shopping cart or checkout page show comments from all the other pages. The comments on the checkout page don't have anything at all to do with the checkout page. They are just other user comments about the site in general. I cannot accept that I have no choice about where comments may or may not display. I can't delete the comments, because they apply to other pages where they were made in the first place.

This screen option in the page edit, for instance, only prevents future comments from appearing. It does nothing to disable existing comments from showing on a checkout page:

When I consult Google about how to remove or hide comments on certain pages, the answers seem to be "all or none." That is, there are plenty of explanations as to how to disable further comments on the page, but nothing on how to make the existing comments not show at all. There is plenty of advice on how to disable comments site-wide, but of course, that's not a serious solution. Suggesting that I delete the comments isn't a solution, because those comments are welcome on other pages.

For instance, our shopping cart or checkout page show comments from all the other pages. The comments on the checkout page don't have anything at all to do with the checkout page. They are just other user comments about the site in general. I cannot accept that I have no choice about where comments may or may not display. I can't delete the comments, because they apply to other pages where they were made in the first place.

This screen option in the page edit, for instance, only prevents future comments from appearing. It does nothing to disable existing comments from showing on a checkout page:

Share Improve this question edited Apr 9, 2022 at 14:35 TARKUS asked Apr 9, 2022 at 13:48 TARKUSTARKUS 13710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There's a filter, wp_count_comments, that might do what you need:

add_filter( 'wp_count_comments', 'wpse_404619_maybe_disable_comments', 10, 2 );

/**
 * Disables comments on certain pages.
 *
 * @param  array $comments The page's comments.
 * @param  int   $post_id  The page's post_ID.
 * @return array           The filtered comments; empty array to disable.
 */
function wpse_404619_maybe_disable_comments( $comments, $post_id ) {
    // Sets the list of page IDs with disabled comments.
    $disable_comments_for_these_pages = array( 1, 2, 5, 6 );
    if ( in_array( $post_id, $disable_comments_for_these_pages ) ) {
        // Returns an empty array to disable comments.
        return array();
    }
    return $comments;
}

This code is untested, but hopefully it'll provide you a starting point.

本文标签: Hide comments on specific pagesnot just disable future comments