几乎WordPress网站都带有站内搜索的功能,我们如何在搜索结果页中显示搜索关键词呢?the_search_query函数能帮我们输出搜索关键词,下面是WordPress函数the_search_query的详细用法。

几乎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中放置如下代码,将会显示通过搜索表单提交的关键词。

<?php the_search_query(); ?>
<?php echo esc_html( get_search_query( false ) ); ?>

该函数不带参数,上面的两种方式输出的结果一样。

声明:本站所有文章为演示内容,如无特殊说明或标注,均为演示内容,无真实下载地址和资源地址,仅供参考,购买组件仅供功能体验。本站所使用主题购买地址为:www.ritheme.com