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 则返回表单的字符串。
安装KB5014697补丁以后开启热点无法上网怎么办?
Windows 11 的最新星期二补丁KB5014697更...(627 )人阅读时间:2025-11-25
Win11本地安全策略在哪里?Win11添加IP 安全策略的
IP安全策略是一个给予通讯分析的策略,当我们配置好IP安全...(948 )人阅读时间:2025-11-25
Win11字体显示不全如何解决?Win11字体显示不全解决方
Win11系统是当前最好用的系统之一,在给用户带来全新界面...(752 )人阅读时间:2025-11-25
Win11怎么创建虚拟磁盘?Win11创建虚拟磁盘的方法
虚拟磁盘就是用内存中虚拟出一个或者多个磁盘的技术。和虚拟内...(607 )人阅读时间:2025-11-25