Almost all WordPress websites come with in site search functionality. How do we display search keywords on the search results page? The the_Sarch_query function can help us output search keywords. Below is a detailed usage of the WordPress function the_Sarch_query.

Almost all WordPress websites come with in site search functionality. How do we display search keywords on the search results page? The the_Sarch_query function can help us output search keywords. Below is a detailed usage of the WordPress function the_Sarch_query.

function description

Display the content of the search query. It should be ensured that it is safe to display on HTML tags through the esc_attr() function.

function prototype

The themesearch_query function is located in the wp-includes/general-template file. Here is its source code, official online address: 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 ) ) ); }
Function Usage

Place the following code in your search template file search.chp, which will display the keywords submitted through the search form.

 

<?php the_search_query(); ?>

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

This function takes no parameters, and the results output by the two methods above are the same.

Disclaimer: All articles on this website, unless otherwise specified or marked, are original and published on this website. Any individual or organization is prohibited from copying, stealing, collecting, or publishing the content of this website to any website, book, or other media platform without the consent of this website. If the content on this website violates the legitimate rights and interests of the original author, please contact us for handling.