comments_open() - WordPress 函数参考中文文档

返回介绍

comments_open()

发布于 2017-09-10 字数4943 浏览 1517 评论 0

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

上一篇:comments_number()

下一篇:comments_popup_link()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!