I’m stuck trying to get search results to show up in my custom WordPress theme. I’ve added the searchform.php to my header and created an empty search.php file. When I search, it takes me to a blank page.
I think I need to add something to search.php, but I’m not sure what. I’ve looked through the WordPress Codex but couldn’t find a clear answer.
Does anyone know what I need to do to make search results appear? Is there a specific function or query I should use?
Hey Grace, I’ve been through this exact headache before. The key is definitely in your search.php file. Here’s what worked for me:
First, make sure you’re calling get_header() at the top of search.php. Then, you’ll want to set up your main content area and include the WordPress loop. Something like this:
Don’t forget to create a content-search.php file in your template-parts folder to control how each search result is displayed. This approach gives you more flexibility in styling your results. Hope this helps!
Grace, I’ve encountered this issue before. Your search.php file needs to include the WordPress loop to display results. Here’s a basic structure to get you started:
<?php get_header(); ?>
<h1>Search Results for: <?php echo get_search_query(); ?></h1>
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
// Your theme's post display code here
// For example: get_template_part('content', 'search');
endwhile;
the_posts_navigation();
else :
echo '<p>Sorry, no posts matched your search.</p>';
endif;
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Customize this to match your theme’s structure and styling. Remember to create a content-search.php file if you use get_template_part().