Last active
August 22, 2016 14:00
-
-
Save dipakcg/ec9934b4a603df82726a to your computer and use it in GitHub Desktop.
WordPress - Display Google Custom Search results if no posts/results found.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* 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); | |
$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 | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment