^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数comments_open()用法示例

comments_open( int|WP_Post $post_id = null )

Whether the current post is open for comments.


描述


参数

$post_id

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

Default value: null


返回值

(bool) True if the comments are open.


源代码

File: wp-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 );
}

更新日志

Version 描述
1.5.0 Introduced.

相关函数

Uses

  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post.php:
    get_post()
  • wp-includes/comment-template.php:
    comments_open

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::create_item_permissions_check()
  • wp-includes/embed.php:
    print_embed_comments_button()
  • wp-includes/comment.php:
    wp_handle_comment_submission()
  • wp-includes/general-template.php:
    feed_links_extra()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::wp_newComment()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::_prepare_page()
  • wp-includes/comment-template.php:
    get_post_reply_link()
  • wp-includes/comment-template.php:
    comment_form()
  • wp-includes/comment-template.php:
    comments_popup_link()
  • wp-includes/comment-template.php:
    get_comment_reply_link()
  • Show 5 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Enqueuing a script only if we’re seeing a single post and comments are open for the current post

    
    /**
     * Enqueue wpdocs_script if viewing a post with comments enabled.
     */
    function wpdocs_scripts(){
    	if ( is_single() && comments_open() ) {
    		// wpdocs_script must have been previously registered via wp_register_script()
    		wp_enqueue_script( 'wpdocs_script' );
    	}
    }
    add_action( 'wp_print_scripts', 'wpdocs_scripts' );
    
  2. With this code you can always disable comments on pages, assuming your theme uses comments_open() to check if the comments are open.
    Note: Comments are disabled on pages by default in 4.3+.

    
    /**
     * 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 wpdocs_comments_open( $open, $post_id ) {
    	$post = get_post( $post_id );
    	if ( 'page' == $post->post_type )
    		$open = false;
    	return $open;
    }
    add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );
    

    With this code you can enable comments on a post that has custom field “Allow Comments” set to 1.

    This is helpful when you have told WordPress to disable comments for posts that are older than X days but wish to enable comments for a handful of old posts.

    
    /**
     * 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 wpdocs_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', 'wpdocs_comments_open', 10, 2 );
    
    
    // Отключаем комментирование
    add_filter( 'comments_open', '__return_false' );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈