Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save topleague/2a17b78887fe5ed990c86b790e44b898 to your computer and use it in GitHub Desktop.

Select an option

Save topleague/2a17b78887fe5ed990c86b790e44b898 to your computer and use it in GitHub Desktop.
Page Template for Google Custom Search Engine In Genesis
<?php
/* Template Name: Google CSE */
//* Force Full-Width Layout
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//* Remove Genesis breadcrumbs
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
//* Add Noindex tag to the page
add_action( 'genesis_meta', 'leaguewp_noindex_page' );
function leaguewp_noindex_page() {
echo '<meta name="robots" content="noindex, follow">';
}
//* Insert Google CSE code into <head> section of webpage
add_action( 'genesis_meta', 'leaguewp_google_cse_meta', 15 );
function leaguewp_google_cse_meta() { ?>
<script>
(function() {
var cx = '010806668626128086493:2iwzqalyslq'; // INSERT YOUR CUSTOM SEARCH ID HERE
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><?php
}
//* Add custom body class
add_filter( 'body_class', 'leaguewp_add_body_class' );
function leaguewp_add_body_class( $classes ) {
$classes[] = 'google-cse';
return $classes;
}
//* Remove standard Genesis loop and insert our custom page content
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'leaguewp_custom_content' );
function leaguewp_custom_content() { ?>
<?php if ((isset($_REQUEST['q'])) && (!empty($_REQUEST['q']))) {
$query = 'Search Results for: ' . $_REQUEST['q'];
}
else {
$query = 'Search Results for: ';
} ?>
<div itemtype="http://schema.org/SearchResultsPage" itemscope="itemscope">
<div class="archive-description">
<h1 class="archive-title"><?php echo($query) ?></h1>
</div>
<div class="entry">
<?php echo get_the_content(); ?>
<gcse:searchresults-only></gcse:searchresults-only>
</div>
</div>
<?php }
//* Run the Genesis loop
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment