Skip to content

Instantly share code, notes, and snippets.

@dipakcg
Last active August 22, 2016 14:00

Revisions

  1. dipakcg revised this gist Oct 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion search.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    /* Below code will display Google Custom Search results if no posts found */
    /* Below code will display Google Custom Search results if no posts/results found */
    /* Add the below code in theme's search.php file where you want to display results from Google */
    $query = get_search_query();
    $query_new =str_replace(' ','%20',$query);
  2. dipakcg renamed this gist Oct 15, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. dipakcg created this gist Oct 15, 2014.
    24 changes: 24 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    /* Below code will display Google Custom Search results if no posts found */
    /* Add the below code in theme's search.php file where you want to display results from Google */
    $query = get_search_query();
    $query_new =str_replace(' ','%20',$query);
    $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&rsz=8&q=".$query_new;

    $body = file_get_contents($url);

    $json = json_decode($body);

    for($x=0;$x<count($json->responseData->results);$x++){
    ?>

    <p>
    <h2> <a href="<?php echo $json->responseData->results[$x]->url; ?>"><?php echo $json->responseData->results[$x]->title; ?> </a> </h2>
    <span> <?php echo $json->responseData->results[$x]->url; ?> </span>
    <h4> <?php echo $json->responseData->results[$x]->content; ?> </h4>
    </p>
    <hr />

    <?php
    }
    ?>