Front-End & Daily

[WordPress] How to determine if comments are enabled for a post

[WordPress] How to determine if comments are enabled for a post

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(); //コメントフィードを読み込み
}

Comments

After reviewing the content, personal information will be omitted before publishing.

Enter your name and email address

Enter if you would like a reply via email.

Personal information provided will not be disclosed. It will only be used for replies.

It will be sent directly. Please confirm and click 'Send'.

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

As an Amazon Associate, Ken earns from qualifying purchases.

Share

Share on Twitter Share on Facebook Share on LINE Share on Hatena Bookmark