php评论模板,评论模板 · WordPress开发手册 · 看云

WordPress会根据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 '

' . $comment->comment_content . '

';

}

} 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模板的示例:

```

/**

* 用于显示评论的模板。

*

* 包含评论和评论表单的页面区域。

*

* @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;

?>

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() ), '' . get_the_title() . '' );

?>

wp_list_comments( array(

'style' => 'ol',

'short_ping' => true,

'avatar_size' => 74,

) );

?>

// Are there comments to navigate through?

if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :

?>

<?php _e( 'Comment navigation', 'twentythirteen' ); ?>

```

## 打破comments.php

以上的comment.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;

?>

```

最后,有一个测试,看看是否有与此帖子相关联的评论。

```

```

## 评论标题

打印出在评论上方显示的标题。

>[warning] 注意:使用`_nx()`翻译方法,因此其他开发人员可以提供替代语言翻译。

```

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() ), '' . get_the_title() . '' );

?>

```

## 评论列表

以下代码片段使用`wp_list_comments()`函数获取注释的有序列表。

```

wp_list_comments( array(

'style' => 'ol',

'short_ping' => true,

'avatar_size' => 74,

) );

?>

```

## 评论分页

检查是否有足够的评论,以便添加评论导航,如果是,创建评论导航。

```

// Are there comments to navigate through?

if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :

?>

<?php _e( 'Comment navigation', 'twentythirteen' ); ?>

```

## 评论是封闭的消息。

如果评论未打开,则显示一条表示它们已关闭的行。

```

```

## 结束

本节结束评论循环,包括注释表单,并关闭评论包装器。

```

```

## 评论分页

如果您有很多评论(这使您的页面很长),那么分页您的评论有很多潜在的好处。 分页有助于提高页面加载速度,特别是在移动设备上。

启用评论分页是分两步完成的。

在WordPress中启用分页评论,方法是进入“Settings”>“Discussion”,并选中“Break comments into pages”框。 您可以输入“每页评论数量”的任意数字。

打开您的comments.php模板文件,并添加以下行,您希望出现评论分页。

```

```

## 替代评论模板

在某些情况下,您可能希望在主题中显示您的评论。 为此,您将构建一个备用文件(例如,short-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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值