溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

WordPress中如何獲取搜索表單

發(fā)布時(shí)間:2021-08-31 11:01:28 來源:億速云 閱讀:114 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)WordPress中如何獲取搜索表單,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

get_search_form 函數(shù)在 WordPress 中是用來提取預(yù)設(shè)的搜索表單或者默認(rèn)的搜索表單的。因?yàn)楣俜竭@個(gè)函數(shù)沒有中文的,所以我就簡(jiǎn)單寫了一下。

描述
get_search_form 函數(shù)在 WordPress 中是用來提取自定義搜索表單或者默認(rèn)的搜索表單的。
顯示自定義表單還是顯示默認(rèn)表單,完全取決于您的主題中是否有search.php文件,
如果有該文件,則自動(dòng)調(diào)用該文件,如果沒有則顯示默認(rèn)的搜索表單。

使用

<?php
  get_search_form($echo = true) 
?>

參數(shù)
$echo 布爾型,用來選擇顯示還是返回變量。
默認(rèn)值:true

實(shí)例
沒你想象的復(fù)雜,其實(shí)就是這么簡(jiǎn)單。

<?php
  get_search_form(); 
?>

這里提一下,如果你需要整合谷歌自定義搜索那些的話,
你只要在你的search.php 文件中將自定義的部分代碼放入即可嘍,當(dāng)然你需要設(shè)定樣式。

函數(shù)源代碼

<?php
 /**
 * Display search form.
 *
 * Will first attempt to locate the searchform.php file in either the child or
 * the parent, then load it. If it doesn't exist, then the default search form
 * will be displayed. The default search form is HTML, which will be displayed.
 * There is a filter applied to the search form HTML in order to edit or replace
 * it. The filter is 'get_search_form'.
 *
 * This function is primarily used by themes which want to hardcode the search
 * form into the sidebar and also by the search widget in WordPress.
 *
 * There is also an action that is called whenever the function is run called,
 * 'get_search_form'. This can be useful for outputting JavaScript that the
 * search relies on or various formatting that applies to the beginning of the
 * search. To give a few examples of what it can be used for.
 *
 * @since 2.7.0
 * @param boolean $echo Default to echo and not return the form.
 */
function get_search_form($echo = true) {
 do_action( 'get_search_form' );
 
 $search_form_template = locate_template('searchform.php');
 if ( '' != $search_form_template ) {
 require($search_form_template);
 return;
 }
 
 $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
 <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
 <input type="text" value="' . get_search_query() . '" name="s" id="s" />
 <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
 </div>
 </form>';
 
 if ( $echo )
 echo apply_filters('get_search_form', $form);
 else
 return apply_filters('get_search_form', $form);
}
?>

關(guān)于“WordPress中如何獲取搜索表單”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI