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( __( '← 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
以上的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( __( '← 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中啟用分頁評論,方法是進入“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()
- 簡介
- 主題開發
- WordPress許可證
- 什么是主題
- 開發環境
- 主題開發示例
- 主題基礎
- 模板文件
- 主樣式表(style.css)
- 文章類型
- 規劃主題文件
- 模板層級
- 模板標簽
- 循環
- 主題函數
- 連接主題文件和目錄
- 使用CSS和JavaScript
- 條件標簽
- 類別,標簽和自定義分類
- 模板文件
- 內容模板文件
- 頁面模板文件
- 附件模板文件
- 自定義內容類型
- 部分和其他模板文件
- 評論模板
- 分類模板
- 404頁面
- 主題功能
- 核心支持的功能
- 管理菜單
- 自定義Headers
- 自定義Logo
- 文章格式
- 置頂文章
- Sidebars
- Widgets
- 導航菜單
- 分頁
- 媒體
- Audio
- Images
- Galleries
- Video
- 精選圖片和縮略圖
- 國際化
- 本地化
- 輔助功能
- 主題選項 – 自定義API
- 定制對象
- 改進用戶體驗的工具
- 定制JavaScript API
- JavaScript / Underscore.js渲染的自定義控件
- 高級用法
- 主題安全
- 數據消毒/逃避
- 數據驗證
- 使用隨機數
- 常見漏洞
- 高級主題
- 子主題
- UI最佳實踐
- JavaScript最佳做法
- 主題單元測試
- 驗證你的主題
- Plugin API Hooks
- 發布你的主題
- 所需的主題文件
- 測試
- 主題評論指南
- 寫文檔
- 提交你的主題到WordPress.org
- 參考文獻
- 模板標簽列表
- 條件標簽列表
- 編碼標準
- HTML編碼標準
- CSS編碼標準
- JavaScript編碼標準
- PHP編碼標準
- 插件開發
- 插件開發簡介
- 什么是插件
- 插件基礎
- 頭部要求
- 包括軟件許可證
- 啟用 / 停用 Hooks
- 卸載方法
- 最佳做法
- 插件安全
- 檢查用戶功能
- 數據驗證
- 保護輸入
- 保護輸出
- 隨機數
- Hooks
- Actions
- Filters
- 自定義Hooks
- 高級主題
- 管理菜單
- 頂級菜單
- 子菜單
- 短代碼
- 基本短碼
- 封閉短碼
- 帶參數的短代碼
- TinyMCE增強型短碼
- 設置
- 設置API
- 使用設置API
- 選項API
- 自定義設置頁面
- 元數據
- 管理帖子元數據
- 自定義元數據
- 渲染元數據
- 自定義文章類型
- 注冊自定義文章類型
- 使用自定義文章類型
- 分類
- 使用自定義分類
- 在WP 4.2+中使用“split術語”
- 用戶
- 創建和管理用戶
- 使用用戶元數據
- 角色和功能
- HTTP API
- JavaScript
- jQuery
- Ajax
- 服務器端PHP和入隊
- Heartbeat API
- 概要
- 計劃任務
- 了解WP-Cron計劃
- 安排WP-Cron 事件
- 將WP-Cron掛接到系統任務計劃程序中
- WP-Cron簡單測試
- 國際化
- 本地化
- 如何國際化您的插件
- 國際化安全
- WordPress.org
- 詳細插件指南
- 規劃您的插件
- 如何使用Subversion
- 插件開發者常見問題
- 開發工具
- Debug Bar 和附加組件
- 輔助插件
- REST API手冊
- 資源
- 文章
- 文章修訂
- 文章類型
- 文章狀態
- 類別
- 標簽
- 頁面
- 評論
- 分類
- 媒體
- 用戶
- 設置
- 使用REST API
- 全局參數
- 分頁
- 鏈接和嵌入
- 發現
- 認證
- 經常問的問題
- 骨干JavaScript客戶端
- 客戶端庫
- 擴展REST API
- 添加自定義端點
- 自定義內容類型
- 修改回應
- 模式
- 詞匯表
- 路由和端點
- 控制器類