user3117731
user3117731

Reputation:

How to display search results in wordpress?

Below is my code of 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(); 

Now nothing is displayed to my search results. Can anyone tell me where am i wrong. Like i have not write code for search. so if there is the problem then where does write code for search and how. please help

Upvotes: 0

Views: 124

Answers (1)

prium
prium

Reputation: 167

Add an if(have_posts()) before the while loop. If you have no search result, you can track that and obviously nothing will display if you have no search result.

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

Upvotes: 1

Related Questions