WordPress获取搜索表单函数get_search_form用法-WordPress开发教程
WordPress函数介绍——获取侧边栏函数get_search_form详解。 函数原型 get_search_form函数位于wp-includes/general-template.php第100行左右,函数原型如下:fu
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 则返回表单的字符串。
原创文章,作者:DavidWu,如若转载,请注明出处:https://www.davidwu.net/archives/121596

服务项目 | 服务内容 | 收费标准(元) |
---|---|---|
开发定制 | WordPress主题/插件开发定制 | (以最终需求为准) |
主题/插件汉化 | 汉化团队WordPress主题/插件,翻译率95% | (以标的主题/插件的句子数量为准) |
服务器环境配置 | 基于您现有服务器,搭建配置网站运行环境,结合我们多年来实战经验,可完美支持WordPress等PHP程序运行,并配置伪静态规则、优化目录权限等问题。服务器我们强烈推荐使用Linux系统。 | 100元/次 |
网站托管 | 若贵站目前尚无技术人员,无法完成服务器环境配置,可选择我们的网站托管服务,直接交付正常运行的WordPress站点,并且无需担心服务器的后续维护工作,一切都由我们来帮您完成。 | 标配套餐:1000元/年/站点 高配套餐:联系客服获取 |
网站加速优化 | 从服务器后端配置优化到WordPress数据库缓存、前端页面缓存、JS和CSS压缩合并,全方位优化网站加载速度,实现秒开。(此服务仅针对(云)服务器/VPS) | 500元/次(仅站内优化200元/次) |
主题配置 | 本站所有主题均支持,可快速实现,若有任何问题可以咨询客服解决,若您希望我们提供配置服务,可选购此服务。 | 英文主题安装 60元/次 汉化主题安装 30元/次 |
HTTPS配置 | HTTPS已经不断普及,并且有着更高的安全性以及SEO上的优待。该服务收取的为服务费,SSL证书产生的费用请自行承担。 | 100元/次 |
网站搬家 | 迁移网站所有文件和数据库信息、网站相关配置的调整、以及迁移中的疑难问题故障排除。 | 标准收费:500元/次 若网站数据量大,需协商 |
网站运维 | 提供整站的运维服务,保证网站正常运行。包含:网站故障定位及排除、网站数据备份和恢复、网站攻击及木马等问题的处理等 | 标准收费:2000元/年 IP 5000以上需协商 |