[WordPress] How to determine if comments are enabled for a post
![[WordPress] How to determine if comments are enabled for a post](img/ogp.png)
Here's how to determine if 'Allow comments' is checked for a page or post in WordPress.
Retrieve with get_post()
You can determine this by referring to the 'comment_status' value in the information retrieved by get_post().
$post_data = get_post();
$comment_status = $post_data->comment_status; //'open'または'closed'
If comments are allowed, it will be 'open'
If not allowed, it will be 'closed'.
If executing outside the loop, add global $post; beforehand.
global $post;
$post_data = get_post();
$comment_status = $post_data->comment_status; //'open'または'closed'
This way, you can load the comment feed only when comments are allowed.
global $post;
$post_data = get_post();
$comment_status = $post_data->comment_status; //'open'または'closed'
if($comment_status == 'open'){
comments_template(); //コメントフィードを読み込み
}




If this was helpful, your support would be greatly appreciated!
All support received will go toward my child’s upbringing.
Author's Baby Registry (Amazon)
Support me on OFUSE
Alternatively, support me by purchasing something through the buttons below
(You don't have to buy the specific item linked.)
Support me via Amazon
Support me via Rakuten
Support me via Yahoo!Shopping
PR