1

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 the search.php template? Creating a page template and use the locate_template filter to get the same results of modify the search.php file is a non-needed extra work. See Creating a Search Page. – cybmeta May 11 '14 at 9:37
  • Thanks Cybnet but I need for the client to be able to change the header banner which can only be done by setting that banner in a Page/Post as far as I am aware? I had started out with the search.php file and based my two-column template on this but then I have no control over the banner image? – SixfootJames May 11 '14 at 10:09
  • With "banner image" do you mean "featured image" for posts/pages? If so, you are right, this image can not be set from the admin area for the "search.php" template file. You can do what you are trying to do or use the search.php template file, modify it for your needs and create a option where the administrator of the site can select the image for the search page. I think the second way is a better approach, but this is just my opinion. – cybmeta May 11 '14 at 11:46
  • Thanks Cybnet, I managed to come right with all the of the above EDITS I made now. – SixfootJames May 11 '14 at 12:08
0

This is what finally worked for me.

To pull in the banner (featured) image from another page, create the search-results page in WordPress and make sure it is excluded from the search results by using the Search Exclude plugin, then add your featured image and pop this into your two-column custom template:

<?php if (is_search()) { 
    $attachment_id = 1711; // attachment ID
    $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id($attachment_id), 'full' ); // returns an array
    if( $image_attributes ) { ?> 
        <div class="top-background" style="background: url(<?php echo $image_attributes[0]; ?>) !important;"></div>             
    <?php }?>
<?php }?>

To force wordpress to make use of the two column custom template as your search results page:

<!-- SEARCH PAGE -->
<?php if (is_search()): ?>
    <section class="entry-content clearfix" itemprop="articleBody">
        <ul>
            <li>
                <h3><a href="<?php echo get_permalink(); ?>"><?php the_title();  ?></a></h3>
                <?php 
                    $content = get_the_content();
                    $trimmed_content = wp_trim_words( $content, 40, '<a href="'. get_permalink() .'"> ...Read More</a>' );
                    echo $trimmed_content;
                 ?>
            </li>
         </ul>
    </section>    
<?php endif ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.