首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

WordPress 核心函數:comments_template() 與 get_search_form() 詳解

     編輯:PHP 相關知識

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>

最佳實踐

  1. 主題中**務必提供** comments.php 和 searchform.php,保持樣式一致性。
  2. 評論模板中使用 wp_list_comments()、comment_form() 等標準函數,兼容嵌套回覆、線程樣式。
  3. 搜索表單建議保留 role="search" 和 screen-reader-text 類,提升無障礙訪問分數。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved