WordPress显示评论是基于主题中的comments.php文件的设置和代码。
简单的评论循环
//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.'; }
comments.php模板包含了从数据库中获取评论并在主题中显示它们所需的所有逻辑。
在我们探讨模板文件之前,你会想知道如何在适当的页面上拉入局部模板文件,如single.php。你将把评论模板标签包在一个条件语句中,这样comments.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;
另一个Comments.php例子
下面是Twenty Thirteen主题中包含的comment.php模板的例子。
<?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 // Are there comments to navigate through? 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 -->
剖析comments.php
上面的comments.php可以分解为以下几个部分,以便更好地理解。
模板头部
开头是关于本模板页的说明信息
<?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() ) : ?>
评论标题
打印出在评论上方的标题。
使用_nx()翻译功能,以便其他开发者可以提供替代语言的翻译。
<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>
评论列表
下面的片段使用wp_list_comments()函数创建一个有序的评论列表。
<ol class="comment-list"> <?php wp_list_comments( array( 'style' => 'ol', 'short_ping' => true, 'avatar_size' => 74, ) ); ?> </ol><!-- .comment-list -->
评论分页
检查是否有足够的评论值得添加评论导航,如果有,则创建评论导航。
<?php // Are there comments to navigate through? 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 ?>
评论已关闭消息
如果评论没有打开,则显示一行消息,说明它们已经关闭。
<?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 -->
评论分页
如果你有很多评论(这使得你的页面很长),那么给评论分页有很多潜在的好处。分页有助于提高页面加载速度,特别是在移动设备上。
启用评论分页分两步进行。
- 在WordPress内启用分页评论,方法是进入设置>讨论 ,并勾选 "将评论分成若干页"。你可以为 "每页顶层评论"输入任何数字。
- 打开你的comments.php模板文件,在你希望出现评论分页的地方添加以下代码。
<div class="pagination"> <?php paginate_comments_links(); ?> </div>
备选评论模板
在某些情况下,你可能希望在主题中以不同方式显示评论。为此,你可以建立一个备用文件(例如short-comments.php),并按如下方式调用它:
<?php comments_template( '/short-comments.php' ); ?>
用于替代评论模板的文件路径应该是相对于当前主题根目录的,并包括任何子文件夹。因此,如果自定义的评论模板在主题内部的一个文件夹中,它在被调用时可能看起来像这样:
<?php comments_template( '/custom-templates/alternative-comments.php' ); ?>
函数参考
- wp_list_comments() : 根据各种参数,包括在管理区设置的参数,显示文章或页面的所有评论
- comment_form() : 此标签输出一个完整的评论表格,供模板内使用
- comments_template() : 加载第一个参数中指定的评论模板
- paginate_comments_links() : 为当前文章的评论创建分页链接
- get_comments() : 检索评论
- get_approved_comments() : 检索给定文章ID的批准评论