简体   繁体   English

为什么<!--?php get_search_form(); ?-->结果是空白页?

[英]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).我正在尝试在我的博客页面上实现搜索功能(该网站是从头开始构建的,未使用 WordPress 主题)。

I added this line of code <div class="search-blog"><?php get_search_form(); ?></div>我添加了这行代码<div class="search-blog"><?php get_search_form(); ?></div> <div class="search-blog"><?php get_search_form(); ?></div> to my archive.php file. <div class="search-blog"><?php get_search_form(); ?></div>到我的 archive.php 文件。 I also added a searchform.php file to my theme's directory using sample code I found online.我还使用在网上找到的示例代码将 searchform.php 文件添加到我的主题目录中。 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).我做了一些研究,并将 search.php 添加到主题目录中(一些建议 search.php 需要实际显示搜索结果)。 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?您可以使用 get_header() function 而不是包含 header 再试一次吗?

well出色地

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

here instead of line 2这里而不是第 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使用以下代码在名为searchform.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>

and then separately a file called search.php with the following:然后单独创建一个名为search.php的文件,其中包含以下内容:

<?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(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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