• Resolved Johnny Bravo

    (@herbiehysteria)


    Hi guys

    I have google custom search implemented on my blog in a 2-page format, it works OK.

    i’m trying to add the number of posts found, and also the search query from the user to be displayed above my search results, i have tried the following php but it seems to only show ’74’ results for EVERY search, and the search query itself does not display between the “” speech marks, my search template:

    <?php
    /*
    Template Name: Search Results
    */
    ?>
    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content-page">
    <h2>Your Google Search Results</h2>
    <?php
    $mySearch =& new WP_Query("s=$s & showposts=-1");
    $num = $mySearch->post_count;
    echo $num.' useful search results for "'; the_search_query();
    ?>
    
    <!-- Place this tag where you want the search results to render -->
    <gcse:searchresults-only></gcse:searchresults-only>
    <!-- Search Results code above this line -->
    </div>
    <?php
     include
    (
    "sidebar2.php"
    )
    ; ?>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    i have also tried:

    <?php the_search_query(); ?>

    and

    <?php echo 'Search Results for "'; the_search_query(); echo '"'; ?>

    before my search results, but it doesn’t seem to pull the search query in.

    i’m stumped, any gcse experts in the house?
    my blog: https://artbyherbie.com

Viewing 2 replies - 1 through 2 (of 2 total)
  • I was able to figure this out by using a good old little trick in the PHP manual. I did a google search to see if there was any way of echoing out form data that was submitted. It turns out that PHP has that functionality built right in.

    https://php.net/manual/en/reserved.variables.get.php

    The variable $_GET automatically returns the term that is being called for in a defined query. So, if I wanted to echo out my current results, I might create a page title for my page template that looks something like this:

    <h1 class="page-title"><?php echo( 'Search Results for: "'); ?><span><?php echo $_GET['q']; ?></span>"</h1>

    In the example above, we utilized echo $_GET['q']; in our php code, which echoed out the term being currently used by our “q” query term. Notice that to use the quotation marks, I used my double quote in the first echo ('Search Results for: "');, and then closed our quotation marks right before closing the h1 tag.

    Hope this helps. You can see a live demo of this technique in action on my own search results page here.

    Thread Starter Johnny Bravo

    (@herbiehysteria)

    noel, thanks, that worked a charm, i edited your code slightly by taking out the CSS class and using <h3> instead:

    <h3><?php echo( 'You searched for: "'); ?><span><?php echo $_GET['q']; ?></span>"</h3>

    thanks! moving onwards..

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Search query and results count not showing in search results’ is closed to new replies.