WordPress中用于获取搜索表单的PHP函数使用解析
get_search_form函数在WordPress中是用来提取预设的搜索表单或者默认的搜索表单的。因为官方这个函数没有中文的,所以我就简单写了一下。
描述
get_search_form函数在WordPress中是用来提取自定义搜索表单或者默认的搜索表单的。
显示自定义表单还是显示默认表单,完全取决于您的主题中是否有search.php文件,
如果有该文件,则自动调用该文件,如果没有则显示默认的搜索表单。
使用
<?php get_search_form($echo=true) ?>
参数
$echo布尔型,用来选择显示还是返回变量。
默认值:true
实例
没你想象的复杂,其实就是这么简单。
<?php get_search_form(); ?>
这里提一下,如果你需要整合谷歌自定义搜索那些的话,
你只要在你的search.php文件中将自定义的部分代码放入即可喽,当然你需要设定样式。
函数源代码
<?php
/**
*Displaysearchform.
*
*Willfirstattempttolocatethesearchform.phpfileineitherthechildor
*theparent,thenloadit.Ifitdoesn'texist,thenthedefaultsearchform
*willbedisplayed.ThedefaultsearchformisHTML,whichwillbedisplayed.
*ThereisafilterappliedtothesearchformHTMLinordertoeditorreplace
*it.Thefilteris'get_search_form'.
*
*Thisfunctionisprimarilyusedbythemeswhichwanttohardcodethesearch
*formintothesidebarandalsobythesearchwidgetinWordPress.
*
*Thereisalsoanactionthatiscalledwheneverthefunctionisruncalled,
*'get_search_form'.ThiscanbeusefulforoutputtingJavaScriptthatthe
*searchreliesonorvariousformattingthatappliestothebeginningofthe
*search.Togiveafewexamplesofwhatitcanbeusedfor.
*
*@since2.7.0
*@paramboolean$echoDefaulttoechoandnotreturntheform.
*/
functionget_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='<formrole="search"method="get"id="searchform"action="'.esc_url(home_url('/')).'">
<div><labelclass="screen-reader-text"for="s">'.__('Searchfor:').'</label>
<inputtype="text"value="'.get_search_query().'"name="s"id="s"/>
<inputtype="submit"id="searchsubmit"value="'.esc_attr__('Search').'"/>
</div>
</form>';
if($echo)
echoapply_filters('get_search_form',$form);
else
returnapply_filters('get_search_form',$form);
}
?>