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

所属分类: 网络编程 / PHP编程 阅读数: 1306
收藏 0 赞 0 分享

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 中move_uploaded_file 上传中文文件名失败

这篇文章主要介绍了PHP 中move_uploaded_file 上传中文文件名失败的原因分析及解决方法 ,需要的朋友可以参考下
收藏 0 赞 0 分享

CentOS7编译安装php7.1的教程详解

这篇文章主要介绍了CentOS7编译安装php7.1的教程详解,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP信号处理机制的操作代码讲解

在本篇文章里小编给大家分享了关于PHP信号处理机制的操作的相关知识点内容,需要的朋友们学习下。
收藏 0 赞 0 分享

ThinkPHP3.2.3框架邮件发送功能图文实例详解

这篇文章主要介绍了ThinkPHP3.2.3框架邮件发送功能,结合图文与实例形式详细分析了基于thinkPHP框架进行邮件发送的相关原理、配置及操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

一文掌握PHP Xdebug 本地与远程调试(小结)

这篇文章主要介绍了一文掌握PHP Xdebug 本地与远程调试(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Laravel路由研究之domain解决多域名问题的方法示例

这篇文章主要介绍了Laravel 路由研究之domain解决多域名问题的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

PHP设计模式之策略模式原理与用法实例分析

这篇文章主要介绍了PHP设计模式之策略模式原理与用法,结合实例形式较为详细的分析了策略模式的概念、原理及php实现与使用策略模式的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php使用lua+redis实现限流,计数器模式,令牌桶模式

这篇文章主要介绍了php使用lua+redis实现限流,计数器模式,令牌桶模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Laravel多域名下字段验证的方法

这篇文章主要给大家介绍了关于Laravel多域名下字段验证的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Laravel具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

PHP中quotemeta()函数的用法讲解

今天小编就为大家分享一篇关于PHP中quotemeta()函数的用法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多