WordPress获取搜索表单函数get_search_form用法
WordPress函数介绍——获取侧边栏函数get_search_form详解。

函数原型
get_search_form函数位于wp-includes/general-template.php第100行左右,函数原型如下:
function get_search_form( $echo = true ) {/**
* Fires before the search form is retrieved, at the start of get_search_form().
*
* @since 2.7.0 as 'get_search_form' action.
* @since 3.6.0
*
* @link https://core.trac.wordpress.org/ticket/19321
*/do_action( 'pre_get_search_form' );$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';/**
* Filters the HTML format of the search form.
*
* @since 3.6.0
*
* @param string $format The type of markup to use in the search form.
* Accepts 'html5', 'xhtml'.
*/$format = apply_filters( 'search_form_format', $format );$search_form_template = locate_template( 'searchform.php' );if ( '' != $search_form_template ) {ob_start();require( $search_form_template );$form = ob_get_clean();} else {if ( 'html5' == $format ) {$form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
<label>
<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search …', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
</label>
<input type="submit" class="search-submit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
</form>';} else {$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<div>
<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
</div>
</form>';}}/**
* Filters the HTML output of the search form.
*
* @since 2.7.0
*
* @param string $form The search form HTML output.
*/$result = apply_filters( 'get_search_form', $form );if ( null === $result )$result = $form;if ( $echo )echo $result;elsereturn $result;}函数介绍
使用该函数将会查找主题目录下searchform.php文件作为搜索表单,如果你的主题没有 searchform.php, WordPress 将使用其内置的搜索表单,默认表单代码如下:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> <div><label class="screen-reader-text" for="s">Search for:</label> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div></form>
请注意,搜索表单需要一个 Get 方式(method=”get” )到你博客的首页,而且文本输入框应该被命名为 s (name=”s”),此外,还必须向上面的例子一样包含 alabel 。
使用方法
<?php get_search_form( $echo ); ?>
如果传入true则输出表单,false 则返回表单的字符串。
上一篇:WordPress主题信息函数wp_get_theme()详解
栏 目:WordPress
下一篇:WordPress获取侧边栏函数get_sidebar用法
本文标题:WordPress获取搜索表单函数get_search_form用法
本文地址:https://zz.feitang.co/CMSjiaocheng/25142.html
您可能感兴趣的文章
- 11-11wordpress导航菜单新窗口打开的设置方法
- 11-11WP-PostViews插件如何修改文章阅读数
- 11-11wordpress附件保存目录改为年月日和上传文件重命名为时间戳
- 11-11WordPress 上传媒体库文件重命名 全格式支持
- 11-11解决wordpress安装后更新或者上传文件权限不足的问题
- 11-11wordpress实现主动推送+熊掌号推送同步进行
- 11-11WordPress 添加友情链接设置 nofollow 属性
- 11-11WordPress 实现 wp_list_bookmarks 自定义友情链接排除调用
- 11-11WordPress首页指定或排除某分类文章显示
- 11-11WordPress不同分类调用不同的文章模板


阅读排行
推荐教程
- 09-22wordpress如何添加描述
- 11-11wordpress附件保存目录改为年月日和上传文件重命名为时间戳
- 10-19纯代码给WordPress网站添加独立下载页面功能
- 11-11WordPress之给文章内容中间插入广告的实现方法
- 10-18wordpress无插件调用随机文章的方法
- 10-18WordPress 技巧:WordPress 后台也使用七牛云存储
- 10-24Win10无法调节声音怎么办?Win10无法调节声音的解决方法
- 10-19WordPress上传图片HTTP错误的解决方法
- 10-18让注册用户可以上传自己的头像的WordPress插件-WP User Avatar v2.
- 11-11国外WordPress精美主题如何DIY汉化成中文版呢?





