Two Different Search Results Pages

I wanted to customize the search results so I created “search.php” in my child theme. I am not done yet but I am noticing that if the search returns a result, I get my customized template. If there is no result I get a completely broken page that doesn’t even use my global header. How do I fix this?

Correct Results

Incorrect Results

Hi There,

Please try with this solution:

Hope it helps :slight_smile:

Thank you, that did help with the problem of having no header. The only issue is that it still doesn’t return anything. If you will notice, my search results page has a search bar at the top as well as a line that tells the user how many results were found. In this instance it should be 0 (zero) but instead brings up what I can only assume to be a completely different template. Do I need to share the search page code or a link or anything? Thank you for your help!

Hello Joshua,

Thanks for the updates.

I have inspected your site and you are completely wrong. The search page with results and with no results is using the same layout template.

With Results:

With No Results:

It seems that there is something in your code that created this issue. Have you coded it yourself or do you have a developer? Please double your conditions within your custom search template to make sure that you will get what you have in mind.

Hope this helps. Please let us know how it goes.

I am not really sure how to double the conditions. Basically, I took the code from another forum on search pages and modified it some to match what I wanted but I didn’t do anything major, just some text additions. I will share the code below.

<?php
get_header();?>


<div id="primary" class="custom-search-page content-area col-md-9">
	<main id="main" class="post-wrap" role="main">

	<?php if ( have_posts() ) : ?>

		<header class="page-header">
				<h3 class="results-header"><?php echo $wp_query->found_posts; ?>
    <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"</h3>
			<div class="custom-search-box">
				<p><?php get_search_form(); ?></p>
				<p>Type your text and hit enter to search...</p>
			</div>
			
			
		</header><!-- .page-header -->
		
	<?php /* Start the Loop */ ?>
		<?php while ( have_posts() ) : the_post(); ?>

		<div class="custom-search-results">
			<div>
	<?php if ( has_post_thumbnail() ): ?>
      <?php the_post_thumbnail( 'medium'); ?>
    <?php endif ?>
			</div>
			<a href="<?php the_permalink();?>"><h3><?php the_title(); ?></h3></a></div>			

			<?php
			/**
			 * Run the loop for the search to output the results.
			 * If you want to overload this in a child theme then include a file
			 * called content-search.php and that will be used instead.
			 */
			
			get_template_part( 'content', 'search' );
			?>

		<?php endwhile; ?>
		<?php the_posts_navigation(); ?>

	<?php else : ?>

		<?php get_template_part( 'content', 'none' ); ?>

	<?php endif; ?>
		
	</main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

Thank you again for your help!

Hey Joshua,

Just as I suspect, the header is not displaying because of how the template layout is coded. Please have it updated and use this:

<?php get_header();?>


<div id="primary" class="custom-search-page content-area col-md-9">
	<main id="main" class="post-wrap" role="main">

		<header class="page-header">
			<h3 class="results-header"><?php echo $wp_query->found_posts; ?>
<?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"</h3>
			<div class="custom-search-box">
				<p><?php get_search_form(); ?></p>
				<p>Type your text and hit enter to search...</p>
			</div>
			
			
		</header><!-- .page-header -->

	<?php if ( have_posts() ) : ?>

		
	<?php /* Start the Loop */ ?>
		<?php while ( have_posts() ) : the_post(); ?>

		<div class="custom-search-results">
			<div>
				<?php if ( has_post_thumbnail() ): ?>
			      <?php the_post_thumbnail( 'medium'); ?>
			    <?php endif ?>
			</div>
			<a href="<?php the_permalink();?>"><h3><?php the_title(); ?></h3></a></div>			

			<?php
			/**
			 * Run the loop for the search to output the results.
			 * If you want to overload this in a child theme then include a file
			 * called content-search.php and that will be used instead.
			 */
			
			get_template_part( 'content', 'search' );
			?>

		<?php endwhile; ?>
		<?php the_posts_navigation(); ?>

	<?php else : ?>

		<?php get_template_part( 'content', 'none' ); ?>

	<?php endif; ?>
		
	</main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>

Please let us know how it goes.

This worked perfectly, thank you!

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.