I am trying to implement a search functionality on my blog page (the website is built from scratch, not using a WordPress theme).
I added this line of code <div class="search-blog"><?php get_search_form(); ?></div>
to my archive.php file. I also added a searchform.php file to my theme’s directory using sample code I found online. Here it is:
<?php /* Template Name: Blog */ ?>
<?php include 'header.php'; ?>
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Blog Posts', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
</label>
<input type="submit" class="search-submit" value="<?php echo esc_attr_x( ' >', 'submit button' ) ?>" />
</form>
<?php include 'footer.php'; ?>
However, when I enter a search term in the search box I get a blank page with only the footer showing. Why? I did some research and also added search.php to the theme’s directory (some suggested search.php is needed to actually show the results of the search). But that made things even worse because then the blog page didn’t load at all.
I am not sure what I am doing wrong here. Any help would be very much appreciated!
3
Answers
Can you try again using the get_header() function instead of including the header?
well
here instead of line 2
can you use this?
With one last overlooked change;
Search term text box type is must be "text"
add a file in your theme called
searchform.php
with the following codeand then separately a file called
search.php
with the following: