I want my client to have control over the look of the Search Results page by being able to select one of the templates I have created for them (page-two-column.php or page-three-column.php) so they can change elements like banner images etc.
So I have created a new page in the WP Dashboard called Search-Results which returns the following mysite.com/search-results/
By default WordPress creates a URL like this mysite.com/?s=product
Having looked through a number of examples and in the codex, I have come across this snippet of code I thought would do what I need it to do.
// Use the two column template as the search result.
add_action('template_include', 'new_search_tmpl');
function new_search_tmpl( $template ) {
if ( is_search() ) {
$t = locate_template('page-two-column.php', false);
if ( ! empty($t) ) $template = $t;
}
return $template;
}
Then, in the page-two-column.php I am checking to see if we're requesting search results like this...
<!-- SEARCH PAGE -->
<?php if (is_search()): ?>
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
<?php else: ?>
<article id="post-not-found" class="hentry clearfix">
<header class="article-header">
<h3><?php _e( "It looks like we couldn't find what you were looking for!", 'bonestheme' ); ?></h3>
</header>
<section class="entry-content">
<p><?php _e( 'Try a different search.', 'bonestheme' ); ?></p>
</section>
<footer class="article-footer">
<p><?php // _e( 'This is the error message in the page.php template.', 'bonestheme' ); ?></p>
</footer>
</article>
<?php endif ?>
Which comes from the original search.php page I am wanting to override.
EDIT Further testing shows that is does actually use the two-col template which is great, however, I still need it to pull the banner image from the search-results page I am creating.
I have now been able to pull the search-results Featured Image into the search page with the following code:
<?php if (is_search()) {
echo get_the_post_thumbnail( '1711', '1920' );
}?>
locate_template
doesn't accept tempalte name, only template file name in array, not as string as you are using. See the docs. Also, why don't modified thesearch.php
template? Creating a page template and use thelocate_template
filter to get the same results of modify thesearch.php
file is a non-needed extra work. See Creating a Search Page.