WordPress中用于获取搜索表单的PHP函数使用解析
更新时间:2016年1月7日 13:52 点击:1793
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); } ?>
相关文章
- 公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
深入解析WordPress中加载模板的get_template_part函数
这篇文章主要介绍了WordPress中加载模板的get_template_part函数,其中重点讲解了其函数钩子的使用,需要的朋友可以参考下...2016-01-14- WordPress插件的加载顺序其实对于很多朋友来讲都没有必要如何来操作了,但有时安装插件太多了我们需要设置一顺序了那么要如何来安装呢,下面来看看. 默认的情况下,Word...2016-11-25
- 这篇文章主要介绍了WordPress中用于获取文章作者与分类信息的方法整理,都是来自于WordPress的WP_Query类之下,需要的朋友可以参考下...2015-12-21
- 这篇文章主要介绍了WordPress中获取所使用的模板的页面ID的简单方法,通过这个方法来获取页面的链接是比较方便的,需要的朋友可以参考下...2016-01-04
- 这篇文章主要介绍了Angular2搜索和重置按钮过场动画,需要的朋友可以参考下...2017-05-27
- 这篇文章主要介绍了在WordPress的后台中添加顶级菜单和子菜单的函数详解,需要的朋友可以参考下...2016-01-14
- 这篇文章主要介绍了WordPress中用于检索模版的相关PHP函数使用解析,包括索模板的函数的使用,要的朋友可以参考下...2015-12-17
- 这篇文章主要介绍了编写PHP脚本来实现WordPress中评论分页的功能的方法,包括上一页下一页和导航式分页功能的添加,需要的朋友可以参考下...2015-12-14
- 带搜索功能下拉框在项目中经常会用到,下面小编把实现代码分享到脚本之家平台,供大家参考...2016-05-13
- 这篇文章主要介绍了WordPress后台中实现图片上传功能的实例讲解,包括多个图片上传表单功能的实现,需要的朋友可以参考下...2016-01-14
- 这篇文章主要介绍了WordPress中用于创建以及获取侧边栏的PHP函数讲解,分别为register_sidebar()函数和get_sidebar()的使用,需要的朋友可以参考下...2016-01-02
WordPress中获取页面链接和标题的相关PHP函数用法解析
这篇文章主要介绍了WordPress中获取页面链接和标题的相关PHP函数用法解析,分别为get_permalink()和wp_title()函数的使用,需要的朋友可以参考下...2015-12-21- 这篇文章主要介绍了WordPress中邮件的一些修改和自定义技巧,包括更改邮件为HTML形式以及定义SMTP邮件的发件人的方法,需要的朋友可以参考下...2015-12-17
- 这篇文章主要介绍了在WordPress中实现发送http请求的相关函数解析,包括使用WP_Http类中的函数来发送post或get请求的方法,需要的朋友可以参考下...2016-01-02
解析WordPress中的post_class与get_post_class函数
这篇文章主要介绍了WordPress中的post_class与get_post_class函数,包括post_class()的PHP源码的相应介绍,需要的朋友可以参考下...2016-01-07解析WordPress中控制用户登陆和判断用户登陆的PHP函数
这篇文章主要介绍了WordPress中控制用户登陆和判断用户登陆的PHP函数,WordPress现在多用户功能已推出了一段时间,针对多用户需求的开发也日益增多,需要的朋友可以参考下...2016-03-07- 这篇文章主要介绍了ThinkPHP使用getlist方法实现数据搜索功能,结合实例形式较为详细的分析了thinkPHP基于getlist实现根据给定条件进行数据的读取、显示等相关操作技巧,需要的朋友可以参考下...2017-05-21
- 这篇文章主要介绍了PHP 搜索查询功能的实现代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-12-02
详解WordPress中分类函数wp_list_categories的使用
这篇文章主要介绍了详解WordPress中分类函数wp_list_categories的使用,文中罗列其主要参数的功能和写法,需要的朋友可以参考下...2016-01-07