简体   繁体   中英

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

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> <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!

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?

With one last overlooked change;

<?php /* Template Name: Blog */ ?>
<?php get_header(); ?>
<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="text" 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 get_footer(); ?>

Search term text box type is must be "text"

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

<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>

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 class="page-title search-page-title">
        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(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM