the_search_query()
云策文档标注
概述
the_search_query() 函数用于显示搜索查询变量的内容,适用于在 HTML 属性中安全输出。它通过 esc_attr() 进行转义,确保安全性,并支持通过 'the_search_query' 过滤器进行自定义。
关键要点
- 显示搜索查询变量的内容,适用于 WordPress 搜索功能。
- 使用 esc_attr() 转义输出,确保在 HTML 属性中安全使用。
- 支持 'the_search_query' 过滤器,允许开发者过滤查询内容。
- 与 get_search_query() 函数相关,用于检索查询变量。
- 自 WordPress 2.1.0 版本引入,兼容性良好。
代码示例
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />注意事项
此函数主要用于前端显示,确保在模板文件中正确调用以输出搜索查询。注意转义处理,避免 XSS 攻击。
原文内容
Displays the contents of the search query variable.
Description
The search query string is passed through esc_attr() to ensure that it is safe for placing in an HTML attribute.
Source
function the_search_query() {
/**
* Filters the contents of the search query variable, for display.
*
* @since 2.3.0
*
* @param mixed $search Contents of the search query variable.
*/
echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) );
}
Hooks
- apply_filters( ‘the_search_query’, mixed $search )
-
Filters the contents of the search query variable, for display.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 3 content
Codex
Show search query in search box
If you have just performed a search, you can show the last query in the search box:
<form method="get" id="searchform" action="<?php bloginfo( 'url' ); ?>/"> <div> <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form>Skip to note 4 content
Codex
Show search query in results page
You can display the search string on search result pages
<p>You searched for " ". Here are the results:</p>