Php 如何在wordpress中显示搜索结果?

Php 如何在wordpress中显示搜索结果?,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,下面是我的search.php代码 get_header(); <p>You searched for " <?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p> while (have_posts()) : the_post(); <h1>Search Results</h1> <a

下面是我的search.php代码

get_header(); 
<p>You searched for " <?php echo esc_html( get_search_query( false ) ); ?> ". Here are the results:</p>


while (have_posts()) : the_post();
        <h1>Search Results</h1>
        <a href="<?php the_permalink()">
            <h2><?php the_title(); ?></h2>
        </a>
        <p><?php the_excerpt(); ?></p>
    <?php endwhile; ?>
<?php get_sidebar(); ?>
<?php get_footer(); 
get_header();
您搜索“”了。结果如下:

while(have_posts()):the_post(); 搜索结果

在while循环之前添加
if(have_posts())
。如果您没有搜索结果,您可以跟踪它,并且如果您没有搜索结果,显然不会显示任何内容

if(have_posts()){
    while(have_posts()){
       the_post();
       ....
       ....
    }
 }
 else{
    echo "No result found!";
 }

请考虑打开PHP和关闭PHP标签,因为当我包含它时。代码似乎毫无疑问,您不允许在链接(a)中包含h2,但除此之外:您的查询看起来如何?(have_posts()可能返回false)您根本没有
完全在那里。