返回介绍

comments_open()

发布于 2017-09-10 21:46:20 字数 4941 浏览 1886 评论 0 收藏 0

comments_open( int|WP_Post $post_id = null )

Whether the current post is open for comments.


description


参数

$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 );
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

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' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文