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 badges1 Answer
Reset to default 1There'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
版权声明:本文标题:Hide comments on specific pages, not just disable future comments 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738636108a2104043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论