首页>文档>WordPress模板标签>comments_template() – 加载评论

comments_template() – 加载评论

模板标签

comments_template( string $file = '/comments.php', bool $separate_comments = false )

加载’$file’中指定的评论模板”comments.php

说明

如果在帖子、页面中或者帖子里没有评论则不显示评论模板。

使用WordPress数据库对象查询评论。评论通过”comments_array”过滤器钩子传递,并分别带有评论列表和帖子ID。

$file路径通过一个名为”comments_template”的过滤器钩子传递,该钩子包括TEMPLATEPATH和$file组合。首先尝试$filtered路径,如果失败,则需要来自默认主题的默认评论模板。如果两者都不存在,那么WordPress进程将被停止。因此,建议不要删除默认主题。

如果帖子没有,将不会尝试获取评论。

参数

$file

字符串 (可选) 要加载的文件。

默认值:’/comments.php’

$separate_comments

bool (可选) 是否按评论类型分隔评论。

默认值:false

来源

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

function comments_template( $file = '/comments.php', $separate_comments = false ) {
    global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
 
    if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
        return;
    }
 
    if ( empty( $file ) ) {
        $file = '/comments.php';
    }
 
    $req = get_option( 'require_name_email' );
 
    /*
     * Comment author information fetched from the comment cookies.
     */
    $commenter = wp_get_current_commenter();
 
    /*
     * The name of the current comment author escaped for use in attributes.
     * Escaped by sanitize_comment_cookies().
     */
    $comment_author = $commenter['comment_author'];
 
    /*
     * The email address of the current comment author escaped for use in attributes.
     * Escaped by sanitize_comment_cookies().
     */
    $comment_author_email = $commenter['comment_author_email'];
 
    /*
     * The URL of the current comment author escaped for use in attributes.
     */
    $comment_author_url = esc_url( $commenter['comment_author_url'] );
 
    $comment_args = array(
        'orderby'                   => 'comment_date_gmt',
        'order'                     => 'ASC',
        'status'                    => 'approve',
        'post_id'                   => $post->ID,
        'no_found_rows'             => false,
        'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
    );
 
    if ( get_option( 'thread_comments' ) ) {
        $comment_args['hierarchical'] = 'threaded';
    } else {
        $comment_args['hierarchical'] = false;
    }
 
    if ( is_user_logged_in() ) {
        $comment_args['include_unapproved'] = array( get_current_user_id() );
    } else {
        $unapproved_email = wp_get_unapproved_comment_author_email();
 
        if ( $unapproved_email ) {
            $comment_args['include_unapproved'] = array( $unapproved_email );
        }
    }
 
    $per_page = 0;
    if ( get_option( 'page_comments' ) ) {
        $per_page = (int) get_query_var( 'comments_per_page' );
        if ( 0 === $per_page ) {
            $per_page = (int) get_option( 'comments_per_page' );
        }
 
        $comment_args['number'] = $per_page;
        $page                   = (int) get_query_var( 'cpage' );
 
        if ( $page ) {
            $comment_args['offset'] = ( $page - 1 ) * $per_page;
        } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
            $comment_args['offset'] = 0;
        } else {
            // If fetching the first page of 'newest', we need a top-level comment count.
            $top_level_query = new WP_Comment_Query();
            $top_level_args  = array(
                'count'   => true,
                'orderby' => false,
                'post_id' => $post->ID,
                'status'  => 'approve',
            );
 
            if ( $comment_args['hierarchical'] ) {
                $top_level_args['parent'] = 0;
            }
 
            if ( isset( $comment_args['include_unapproved'] ) ) {
                $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
            }
 
            $top_level_count = $top_level_query->query( $top_level_args );
 
            $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
        }
    }
 
    /**
     * Filters the arguments used to query comments in comments_template().
     *
     * @since 4.5.0
     *
     * @see WP_Comment_Query::__construct()
     *
     * @param array $comment_args {
     *     Array of WP_Comment_Query arguments.
     *
     *     @type string|array $orderby                   Field(s) to order by.
     *     @type string       $order                     Order of results. Accepts 'ASC' or 'DESC'.
     *     @type string       $status                    Comment status.
     *     @type array        $include_unapproved        Array of IDs or email addresses whose unapproved comments
     *                                                   will be included in results.
     *     @type int          $post_id                   ID of the post.
     *     @type bool         $no_found_rows             Whether to refrain from querying for found rows.
     *     @type bool         $update_comment_meta_cache Whether to prime cache for comment meta.
     *     @type bool|string  $hierarchical              Whether to query for comments hierarchically.
     *     @type int          $offset                    Comment offset.
     *     @type int          $number                    Number of comments to fetch.
     * }
     */
    $comment_args  = apply_filters( 'comments_template_query_args', $comment_args );
    $comment_query = new WP_Comment_Query( $comment_args );
    $_comments     = $comment_query->comments;
 
    // Trees must be flattened before they're passed to the walker.
    if ( $comment_args['hierarchical'] ) {
        $comments_flat = array();
        foreach ( $_comments as $_comment ) {
            $comments_flat[]  = $_comment;
            $comment_children = $_comment->get_children(
                array(
                    'format'  => 'flat',
                    'status'  => $comment_args['status'],
                    'orderby' => $comment_args['orderby'],
                )
            );
 
            foreach ( $comment_children as $comment_child ) {
                $comments_flat[] = $comment_child;
            }
        }
    } else {
        $comments_flat = $_comments;
    }
 
    /**
     * Filters the comments array.
     *
     * @since 2.1.0
     *
     * @param array $comments Array of comments supplied to the comments template.
     * @param int   $post_ID  Post ID.
     */
    $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
 
    $comments                        = &$wp_query->comments;
    $wp_query->comment_count         = count( $wp_query->comments );
    $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
 
    if ( $separate_comments ) {
        $wp_query->comments_by_type = separate_comments( $comments );
        $comments_by_type           = &$wp_query->comments_by_type;
    } else {
        $wp_query->comments_by_type = array();
    }
 
    $overridden_cpage = false;
 
    if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
        set_query_var( 'cpage', 'newest' === get_option( 'default_comments_page' ) ? get_comment_pages_count() : 1 );
        $overridden_cpage = true;
    }
 
    if ( ! defined( 'COMMENTS_TEMPLATE' ) ) {
        define( 'COMMENTS_TEMPLATE', true );
    }
 
    $theme_template = STYLESHEETPATH . $file;
 
    /**
     * Filters the path to the theme template file used for the comments template.
     *
     * @since 1.5.1
     *
     * @param string $theme_template The path to the theme template file.
     */
    $include = apply_filters( 'comments_template', $theme_template );
 
    if ( file_exists( $include ) ) {
        require $include;
    } elseif ( file_exists( TEMPLATEPATH . $file ) ) {
        require TEMPLATEPATH . $file;
    } else { // Backward compat code will be removed in a future release.
        require ABSPATH . WPINC . '/theme-compat/comments.php';
    }
}

更多

默认用法

<?php comments_template(); ?>

替代评论模板

在某些情况下,您可能希望在主题中以不同的方式显示评论。为此,您将构建一个备用文件(例如short-comments.php),并按以下方式调用它:

<?php comments_template( '/short-comments.php' ); ?>

用于替代注释模板的文件的路径应相对于当前主题根目录,并包括所有子文件夹。因此,如果自定义评论模板位于主题内的文件夹中,则在调用时可能看起来像这样:

<?php comments_template( '/custom-templates/alternative-comments.php' ); ?>

在page.php和single.php中使用

// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
    comments_template();
endif;

官方原文档

https://developer.wordpress.org/reference/functions/comments_template/

个人中心
今日签到
有新私信 私信列表
搜索