comments_template() — 載入評論模板
comments_template( $file = '/comments.php', $separate_comments = false );
- $file:相對當前主題根目錄的模板文件,默認
comments.php。可傳 '/short-comments.php' 載入自定義模板。
- $separate_comments:是否按類型(普通評論、引用、trackback)分離顯示,默認
false。
- **無返回值**,直接輸出。
- 僅在單篇文章(
is_single())或頁面(is_page())中生效。
使用示例
<?php comments_template(); ?> <!-- 載入默認 comments.php -->
<?php comments_template('/short-comments.php'); ?> <!-- 載入自定義模板 -->
get_search_form() — 獲取搜索表單 HTML
get_search_form( $echo = true );
- $echo:
true 直接輸出,false 返回字符串供變量接收。
- 優先查找主題根目錄
searchform.php,不存在則輸出內置默認表單(HTML5 結構,含 role="search"、label、input[name="s"]、提交按鈕)。
使用示例
<?php get_search_form(); ?> <!-- 直接輸出 -->
<?php $form = get_search_form(false); ?> <!-- 獲取字符串進一步處理 -->
默認搜索表單結構(無 searchform.php 時)
<form role="search" method="get" id="searchform" class="searchform" action="<?= esc_url(home_url('/')) ?>">
<div>
<label class="screen-reader-text" for="s"><?= _x('Search for:', 'label') ?></label>
<input type="text" value="<?= get_search_query() ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?= esc_attr_x('Search', 'submit button') ?>" />
</div>
</form>
最佳實踐
- 主題中**務必提供**
comments.php 和 searchform.php,保持樣式一致性。
- 評論模板中使用
wp_list_comments()、comment_form() 等標準函數,兼容嵌套回覆、線程樣式。
- 搜索表單建議保留
role="search" 和 screen-reader-text 類,提升無障礙訪問分數。