WordPress 通过主题中的 comments.php 文件来显示评论,该文件包含从数据库获取评论并展示的逻辑。文档介绍了如何创建和使用评论模板,包括基本循环、条件加载、分页和替代模板。
// 简单评论循环示例
$args = array(
'status' => 'approve',
);
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query( $args );
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p>';
}
} else {
echo 'No comments found.';
}
// 条件加载评论模板
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
// 替代评论模板调用
comments_template( '/short-comments.php' );WordPress displays comments in your theme based on the settings and code in the comments.php file within your WordPress theme.
// Get only the approved comments
$args = array(
'status' => 'approve',
);
// The comment Query
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query( $args );
// Comment Loop
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p>';
}
} else {
echo 'No comments found.';
}
The comments.php template contains all the logic needed to pull comments out of the database and display them in your theme.
Before we explore the template file you’ll want to know how to pull in the partial template file on the appropriate pages such as single.php. You’ll wrap the comment template tag in a conditional statement so comments.php is only pulled in if it makes sense to do.
// 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;

Here’s an example of the comments.php template included with the Twenty Thirteen theme:
<?php
/**
* The template for displaying Comments.
*
* The area of the page that contains comments and the comment form.
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/
/*
* If the current post is protected by a password and the visitor has not yet
* entered the password we will return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<?php if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf(
_nx(
'One thought on "%2$s"',
'%1$s thoughts on "%2$s"',
get_comments_number(),
'comments title',
'twentythirteen'
),
number_format_i18n( get_comments_number() ),
'<span>' . get_the_title() . '</span>'
);
?>
</h2>
<ol class="comment-list">
<?php
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 74,
) );
?>
</ol><!-- .comment-list -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav class="navigation comment-navigation" role="navigation">
<h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h1>
<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentythirteen' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentythirteen' ) ); ?></div>
</nav><!-- .comment-navigation -->
<?php endif; // Check for comment navigation ?>
<?php if ( ! comments_open() && get_comments_number() ) : ?>
<p class="no-comments"><?php _e( 'Comments are closed.', 'twentythirteen' ); ?></p>
<?php endif; ?>
<?php endif; // have_comments() ?>
<?php comment_form(); ?>
</div><!-- #comments -->
The above comments.php can be broken down to the below parts for better understanding.
This template begins by identifying the template.
<?php
/**
* The template for displaying Comments.
*
* The area of the page that contains comments and the comment form.
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/
Next, there’s a test to see if the post is password protected and, if so, it stops processing the template.
/*
* If the current post is protected by a password and the visitor has not yet
* entered the password we will return early without loading the comments.
*/
if ( post_password_required() )
return;
?>
Finally, there’s a test to see if there are comments associated with this post.
<div id="comments" class="comments-area">
<?php if ( have_comments() ) : ?>
Prints out the header that appears above the comments.
<h2 class="comments-title">
<?php
printf(
_nx(
'One thought on "%2$s"',
'%1$s thoughts on "%2$s"',
get_comments_number(),
'comments title',
'twentythirteen'
),
number_format_i18n( get_comments_number() ),
'<span>' . get_the_title() . '</span>'
);
?>
</h2>
The following snippet creates an ordered listing of comments using the wp_list_comments() function.
<ol class="comment-list">
<?php
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 74,
) );
?>
</ol><!-- .comment-list -->
Checks to see if there are enough comments to merit adding comment navigation and, if so, create comment navigation.
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav class="navigation comment-navigation" role="navigation">
<h3 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h3>
<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentythirteen' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentythirteen' ) ); ?></div>
</nav><!-- .comment-navigation -->
<?php endif; // Check for comment navigation ?>
If comments aren’t open, displays a line indicating that they’re closed.
<?php if ( ! comments_open() && get_comments_number() ) : ?>
<p class="no-comments"><?php _e( 'Comments are closed.', 'twentythirteen' ); ?></p>
<?php endif; ?>
This section ends the comments loop, includes the comment form, and closes the comment wrapper.
<?php endif; // have_comments() ?>
<?php comment_form(); ?>
</div><!-- #comments -->
If you have a lot of comments (which makes your page long), then there are a number of potential benefits to paginating your comments. Pagination helps improve page load speed, especially on mobile devices.
Enabling comments pagination is done in two steps.
comments.php template file and add the following line where you want the comment pagination to appear.<div class="pagination">
<?php paginate_comments_links(); ?>
</div>
On some occasions you may want display your comments differently within your theme. For this you would build an alternate file (ex. short-comments.php) and call it as follows:
<?php comments_template( '/short-comments.php' );
The path to the file used for an alternative comments template should be relative to the current theme root directory, and include any subfolders. So if the custom comments template is in a folder inside the theme, it may look like this when called:
<?php comments_template( '/custom-templates/alternative-comments.php' );