skip to Main Content

I’m trying to get the search term within a woocommerce page to display it dynamically on the results page so the user knows what term they searched on.

ex. “Showing results for $searchTerm”

Is there a way to get the search term from the page that was used?

2

Answers


  1. Chosen as BEST ANSWER

    I got this working through shortcodes by editing the functions.php file to include:

    add_shortcode('searchterm', 'get_searchterm');
    
    function get_searchterm() {
    
       return $_GET['ixwpss'];
    
    }
    

    Then I could use [searchterm] as a shortcode on the page. Unfortunately I had to "touch" the php file.


  2. You can make use of the get_search_query() and is_search() functions like this:

    add_action( 'woocommerce_before_main_content', 'show_search_query_on_page', 1 );
    function show_search_query_on_page() {
        if(is_search()) {
            echo '<h2>Showing results for: '.get_search_query().'</h2>';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search