重要:本文最后更新于2018-12-31 11:26:10
,某些文章具有时效性,若有错误或已失效,请在下方留言或联系九月网。
几乎WordPress网站都带有站内搜索的功能,我们如何在搜索结果页中显示搜索关键词呢?the_search_query函数能帮我们输出搜索关键词,下面是WordPress函数the_search_query的详细用法。
函数描述
显示搜索查询的内容。应通过esc_attr()函数确保它是安全的再显示在HTML标签上。
函数原型
the_search_query函数位于wp-includes/general-template.php文件中,下面是它的源代码,官方在线地址:https://developer.wordpress.org/reference/functions/the_search_query/
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 ) ) ); }
函数用法
在你的搜索模板文件search.php中放置如下代码,将会显示通过搜索表单提交的关键词。
该函数不带参数,上面的两种方式输出的结果一样。