Home > front end >  Why does <?php get_search_form(); ?> results in a blank page?
Why does <?php get_search_form(); ?> results in a blank page?

Time:01-14

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 ><?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"  action="<?php echo home_url( '/' ); ?>">
    <label>
        <span ><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search"  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"  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!

CodePudding user response:

Can you try again using the get_header() function instead of including the header?

well

<?php /* Template Name: Blog */ ?>
<?php include 'header.php'; ?>

here instead of line 2

<?php get_header(); ?>

can you use this?

CodePudding user response:

With one last overlooked change;

<?php /* Template Name: Blog */ ?>
<?php get_header(); ?>
<form role="search" method="get"  action="<?php echo home_url( '/' ); ?>">
<label> <span ><?php echo _x( 'Search for:', 'label' ) ?></span> 
<input type="text"  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"  value="<?php echo esc_attr_x( ' >', 'submit button' ) ?>" />
</form>
<?php get_footer(); ?>

Search term text box type is must be "text"

CodePudding user response:

add a file in your theme called searchform.php with the following code

<form role="search" method="get"  action="<?php echo home_url( '/' ); ?>">
    <label>
        <span ><?php echo _x( 'Search for:', 'label' ) ?></span>
        <input type="search"  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"  value="<?php echo esc_attr_x( ' >', 'submit button' ) ?>" />
</form>

and then separately a file called search.php with the following:

<?php get_header(); ?>
    <?php //this prints out your search form 
    get_search_form(); ?>
    <h1 >
        search results for <?php echo get_search_query(); ?>
    </h1>
    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php the_title(); ?>
        <?php endwhile; ?>
    <?php else: ?>
        No results for "<?php echo get_search_query(); ?>
    <?php endif; ?>


<?php get_footer(); ?>
  • Related