WordPress中用于获取搜索表单的PHP函数使用解析


Posted in PHP onJanuary 05, 2016

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
 /**
 * 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);
}
?>
PHP 相关文章推荐
PHP分页显示制作详细讲解
Oct 09 PHP
Session的工作方式
Oct 09 PHP
个人站长制做网页常用的php代码
Mar 03 PHP
单点登录 Ucenter示例分析
Oct 29 PHP
php中adodbzip类实例
Dec 08 PHP
PHP在innodb引擎下快速代建全文搜索功能简明教程【基于xunsearch】
Oct 14 PHP
ThinkPHP 整合Bootstrap Ajax分页样式
Dec 23 PHP
yii2利用自带UploadedFile实现上传图片的示例
Feb 16 PHP
浅谈PHP的exec()函数无返回值排查方法(必看)
Mar 31 PHP
php利用fsockopen GET/POST提交表单及上传文件
May 22 PHP
php readfile()修改文件上传大小设置
Aug 11 PHP
laravel框架中视图的基本使用方法分析
Nov 23 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
Jan 05 #PHP
详解WordPress中调用评论模板和循环输出评论的PHP函数
Jan 05 #PHP
简单的php+mysql聊天室实现方法(附源码)
Jan 05 #PHP
基于PHP实现简单的随机抽奖小程序
Jan 05 #PHP
详解WordPress开发中的get_post与get_posts函数使用
Jan 04 #PHP
使用PHP实现微信摇一摇周边红包
Jan 04 #PHP
解析WordPress中的post_class与get_post_class函数
Jan 04 #PHP
You might like
php 全文搜索和替换的实现代码
2008/07/29 PHP
php cookis创建实现代码
2009/03/16 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
Zend Framework连接Mysql数据库实例分析
2016/03/19 PHP
php 自定义错误日志实例详解
2016/11/12 PHP
php 从指定数字中获取随机组合的简单方法(推荐)
2017/04/05 PHP
thinkPHP5框架接口写法简单示例
2019/08/05 PHP
SUN的《AJAX与J2EE》全文译了
2007/02/23 Javascript
jquery 插件 web2.0分格的分页脚本,可用于ajax无刷新分页
2008/12/25 Javascript
jQuery 学习入门篇附实例代码
2010/03/16 Javascript
在JavaScript中获取请求的URL参数[正则]
2010/12/25 Javascript
js(jQuery)获取时间的方法及常用时间类搜集
2013/10/23 Javascript
js判断设备是否为PC并调整图片大小
2014/02/12 Javascript
jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证
2015/06/25 Javascript
jquery实现最简单的滑动菜单效果代码
2015/09/12 Javascript
实例讲解jquery与json的结合
2016/01/07 Javascript
Node.js返回JSONP详解
2016/05/18 Javascript
VUE长按事件需求详解
2017/10/18 Javascript
Vue 父子组件的数据传递、修改和更新方法
2018/03/01 Javascript
angularJs在多个控制器中共享服务数据的方法
2018/09/30 Javascript
微信小程序-form表单提交代码实例
2019/04/29 Javascript
layer设置maxWidth及maxHeight解决方案
2019/07/26 Javascript
解决在layer.open中使用时间控件laydate失败的问题
2019/09/11 Javascript
Python httplib模块使用实例
2015/04/11 Python
Python使用BeautifulSoup库解析HTML基本使用教程
2016/03/31 Python
对Python3中bytes和HexStr之间的转换详解
2018/12/04 Python
python实现公司年会抽奖程序
2019/01/22 Python
解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path
2019/06/12 Python
深入了解Django中间件及其方法
2019/07/26 Python
Python如何给你的程序做性能测试
2020/07/29 Python
图解Python中深浅copy(通俗易懂)
2020/09/03 Python
python中not、and和or的优先级与详细用法介绍
2020/11/03 Python
高校自主招生教师推荐信
2015/03/23 职场文书
高中家长意见怎么写
2015/06/03 职场文书
人与自然的观后感
2015/06/18 职场文书
python编写五子棋游戏
2021/05/25 Python