外贸独立站的底层设计密码WordPress 成品站模板

评论模板文件

  1. 主页
  2. 文档
  3. WordPress主题开发手册
  4. 模板文件
  5. 评论模板文件

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.';
}

comments.php模板文件包含了从数据库中获取评论,并将它们在主题中显示出来的所有逻辑代码。

在探索模板文件之前,您需要了解如何在适当的页面(例如 single.php )上引入部分模板文件。您需要把模板标记 包含在条件语句中,以便只有在合理的情况下才将comment.php引入。

// 如果允许评论,并且评论的数量大于1,才加载评论模板文件
 if ( comments_open() || get_comments_number() ) :
     comments_template();
 endif;

另一个Comments.php示例

以下是Twenty Thirteen主题中包含的comments.php模板的示例:

<?php
/**
 * 用于显示评论的模板。
 *
 * 包含评论和评论表单的页面区域。
 *
 * @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可以细分为以下部分,以便更好地理解。

  1. 模板标头
  2. 评论标题
  3. 评论列表
  4. 评论分页
  5. 评论关闭的消息
  6. 结束

模板标头

用于定义一个模板。

<?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( __( '&larr; Older Comments', 'twentythirteen' ) ); ?></div>
    <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', '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 -->

评论分页

如果您有很多评论(这会使您的页面很长),那么对您的评论进行分页就非常有必要了。分页有助于提高页面加载速度,尤其是在移动设备上。可以分两步启用评论分页。

  1. 访问后台“设置 >讨论”,然后选中“分页显示评论 ” 框,在WordPress中启用分页评论。您可以为“ 每页显示评论数 ”设置任何数字。
  2. 打开您的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' ); ?>

函数参考

检索评论元素的函数参考

这篇文章对您有用吗?
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索