企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
WordPress会根据WordPress主题中的comments.php文件中的设置和代码在主题中显示注释。 ## 简单的评论循环 ``` <?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只有在有意义的情况下被拉入。 ```php // 如果发表评论,或者至少有一个评论,请加载评论模板。 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( __( '&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 --> ``` ## 打破comments.php 以上的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() ) : ?> ``` ## 评论标题 打印出在评论上方显示的标题。 >[warning] 注意:使用`_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"> <h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h1> <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 --> ``` ## 评论分页 如果您有很多评论(这使您的页面很长),那么分页您的评论有很多潜在的好处。 分页有助于提高页面加载速度,特别是在移动设备上。 启用评论分页是分两步完成的。 在WordPress中启用分页评论,方法是进入“Settings”>“Discussion”,并选中“Break comments into pages”框。 您可以输入“每页评论数量”的任意数字。 打开您的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的批准注释。 ## 检索评论元素的函数参考 - get_comment_link() - get_comment_author() - get_comment_date() - get_comment_time() - get_comment_text()