comments_open()

最后更新于:2021-11-25 20:38:20

comments_open( int|GC_Post$post_id=null)

Determines whether the current post is open for comments.

参数

$post_id

(int|GC_Post) (Optional) Post ID or GC_Post object. Default current post.

Default value: null

响应

(bool) True if the comments are open.

源文件

文件: gc-includes/comment-template.php

function comments_open( $post_id = null ) {

	$_post = get_post( $post_id );

	$post_id = $_post ? $_post->ID : 0;
	$open    = ( 'open' === $_post->comment_status );

	/**
	 * Filters whether the current post is open for comments.
	 *
	 * @since 2.5.0
	 *
	 * @param bool $open    Whether the current post is open for comments.
	 * @param int  $post_id The post ID.
	 */
	return apply_filters( 'comments_open', $open, $post_id );
}
/**
 * Disable comments on pages.
 *
 * @param bool $open    Whether comments should be open.
 * @param int  $post_id Post ID.
 * @return bool Whether comments should be open.
 */
function gcdocs_comments_open( $open, $post_id ) {
	$post = get_post( $post_id );
	if ( 'page' == $post->post_type )
		$open = false;
	return $open;
}
add_filter( 'comments_open', 'gcdocs_comments_open', 10, 2 );
/**
 * Enable or disable comments based on custom field Allow Comments.
 *
 * @param bool $open    Whether comments should be open.
 * @param int  $post_id Post ID.
 * @return bool Whether comments should be open.
 */
function gcdocs_comments_open( $open, $post_id ) {
	$post = get_post( $post_id );
        if (get_post_meta($post->ID, 'Allow Comments', true)) {
		$open = true;
	}
	return $open;
}
add_filter( 'comments_open', 'gcdocs_comments_open', 10, 2 );