WordPress中用于获取搜索表单的PHP函数使用解析_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
, wordpress
搜索表单
wordpress表单插件、wordpress联系表单、wordpress 表单、wordpress提交表单、wordpress 自定义表单,以便于您获取更多的相关知识。

时间: 2024-08-03 22:11:01

WordPress中用于获取搜索表单的PHP函数使用解析_php技巧的相关文章

WordPress中用于获取文章信息以及分类链接的函数用法_php技巧

get_post()(获取一篇文章)get_post() 函数可以根据 ID 查询一篇文章的信息,还能返回循环中的当前文章. 用法 get_post( $post, $output, $filter ); 参数 $id (整数 | 对象)(可选)文章 ID 或者文章对象,如果为空则自动设置成当前文章. 默认值:null(当前文章) $output (字符串)(可选)返回结果的形式,可选: OBJECT:返回一个文章对象 ARRAY_A:返回带键值的数组 ARRAY_N:返回不带键值的数组 默认值

简介WordPress中用于获取首页和站点链接的PHP函数_php技巧

home_url()(获取首页链接)ome_url() 函数用来获取 WordPress 的首页链接. 用法 home_url( $path, $scheme ); 参数 $path (字符串)(可选)在首页链接后边追加的内容,是相对链接. 默认值:None $scheme (字符串)(可选)链接协议,只支持 "http","https" 和 "relative". 默认值:null 返回值 (字符串)返回首页 URL 加上 $path 参数.

WordPress开发中的get_post_custom()函数使用解析_php技巧

同get_post_meta()一样,用于返回文章的自定义字段值得一个函数,只不过get_post_custom()函数使用起来更简单,如果在循环中使用你甚至不需要设置任何参数. 其实get_post_custom()函数的基础实现与get_post_meta()大同小异~ get_post_custom()使用 get_post_custom($postid); 只接受一个参数 $postid文章id: 实例演示 if (have_posts()) : while (have_posts())

在WordPress中使用PHP脚本来判断访客来自什么国家_php技巧

区分访客国家有什么用? 这里是几个我利用该功能的例子. 1.区分网站功能 这个博客有翻译文章的功能, 这是为了方便海外访客阅读文章, 但对中国人显得十分多余. 所以我通过 IP 判断国家, 对中国大陆地区屏蔽翻译功能. 2.区分展示广告 比如中国大陆地区在侧边栏最下方看到的是拿福能的广告, 而其他地区看到的是 Google 的广告. hostucan 是我的一个广告主, 有英文网站, 也有中文网站, 所以我可以向他提供区分展示服务, 免得浪费流量. 3.屏蔽布点服务 海外有很多好的服务平台, 在

php中数字、字符与对象判断函数用法实例_php技巧

本文实例讲述了php中数字.字符与对象判断函数用法.分享给大家供大家参考.具体分析如下: 在php判断数字,字符,对象,数组等包括有参见 is_bool().is_int().is_integer().is_float().is_real().is_object() 和 is_array()这些函数了,不知道你知道多少呢. 1. 双精度数判断:is_double is_double -- is_float() 的别名 描述:此函数是 is_float() 的别名函数,代码如下: 复制代码 代码如

WordPress中用于获取及自定义头像图片的PHP脚本详解_php技巧

get_avatar()(获取头像)get_avatar() 函数用来获取置顶邮箱或者用户的头像代码,在评论列表中非常常用. 这个函数提供一个 get_avatar 过滤器,用来过滤头像的 Html 代码(img 标签). 如果在后台 "设置" 的 "讨论" 里关闭 "显示头像选项" 则返回 False. 用法 get_avatar( $id_or_email, $size, $default, $alt ); 参数 $id_or_email (

WordPress中用于获取文章作者与分类信息的方法整理_php实例

作者查询和某些作者(用户)有关的文章,可以使用 4 个参数: author(整数):用户 ID author_name(字符串):用户的昵称("user_nicename" 字段) author__in(数组):用户 ID author__not_in(数组):用户 ID 获取一个作者的文章 根据用户 ID 获取: $query = new WP_Query( 'author=123' ); 根据用户的昵称("user_nicename" 字段)获取: $query

WordPress中获取页面链接和标题的相关PHP函数用法解析_php技巧

get_permalink()(获取文章或页面链接)get_permalink() 用来根据固定连接返回文章或者页面的链接.在获取链接时 get_permalink() 函数需要知道要获取的文章的 ID,如果在循环中则自动默认使用当前文章. 用法 get_permalink( $id, $leavename ); 参数 $id (混合)(可选)文章或者页面的 ID(整数):还可以是文章对象. 默认值:在循环中自动调用当前的文章 $leavename (布尔)(可选)转化成链接是是否忽略文章别名.

WordPress中用于更新伪静态规则的PHP代码实例讲解_php实例

flush_rewrite_rules() 函数用来删除然后根据现有的条件重写伪静态规则,也就是刷新一次伪静态规则了. 先来说一下,通常在主题或者插件添加新的自定义文章类型的时候调用,防止新的自定义文章类型的文章出现 404 的情况,或者很多时候我们都需要在主题启用的时候执行一些代码,比如布置一些数据库表单.跳转到设置页面等等,WordPress 本身并没有提供相关的钩子,网上也有很多五花八门的实现方法,经过我的研究,发现了可能是最优的方法,下边分享给大家: /** *WordPress 在主题