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

yipeiwu_com5年前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
 /**
 * 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 5.6版本中编写一个PHP扩展的简单示例

有时候在php本身没有满足需求的api时候,需要自己写相应的扩展,扩展写完之后进行编译,即可加入自己的开发环境中,扩展php的功能。 这里实现一个连接字符串和int型数的连接操作的简单扩...

PHP 得到根目录的 __FILE__ 常量

1。PHP 的 __FILE__ 常量(如何得到根目录) dirname(__FILE___) 函数返回的是脚本所在在的路径。 比如文件 ...

一些php技巧与注意事项分析

从浏览器上看,使用 header(location) 就跳转到另一个页面了,但事实上,php却仍然会执行后台的代码的,假如后面的代码有不安全逻辑的,那就直接无视开发者设定的条件,继续把后...

php计算给定时间之前的函数用法实例

本文实例讲述了php计算给定时间之前的函数用法。分享给大家供大家参考。具体如下: 这里给定一个时间,计算这个时间在多久前,比如:2天前,1年前 <?php functi...

Laravel中log无法写入问题的解决

前言 账号登录报500错误,也没有返回错误信息,没办法只能使用原始方法,到现在一行一行打印。到 Log::info() 后面就无法正常显示了,那么问题就找到了。 导致无法写入日志的问题,...