WordPress: No search results message

I’ve tried to separate search results by categories in 2 tabs. Everything works fine except showing the message “No results”. This message is shown only when in both categories are nothing found. But when one tab has results and another hasn’t – nothing is shown.

I’m looking for way to show “No results” for every tab. I mean, if nothing is found in cat 1 and some results found in cat 2 -> Show “No results” in Tab 1 and show results in Tab 2.

Read More

Any suggestions?

Code here:

<div id="tab-content1" class="tab-content">
                  <ul class="posts--group v-category-games">

                    <?php 
                        global $post; 
                        rewind_posts();
                        $query = new WP_Query(array(
                        'posts_per_page' => -1,
                        'cat' => 3,
                        's' => get_search_query(),
                        ));

                        if (have_posts()) :         
                            while ($query->have_posts()) : $query->the_post(); 
                    ?>

                    <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>       

                        <li>
                            <div class="post-item v-category-games v-with-image">
                                <div class="post-item--text">
                                    <a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    <span class="post-item--text--tagline">
                                        <span><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?> </a></span>
                                        <span> </span>
                                    </span>
                                </div>

                                <div class="post-item--thumbnail">

                                    <div class="post-thumbnail">
                                        <div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
                                        <span></span>
                                        <span></span>
                                    </div>
                                </div>
                            </div>           
                        </li>

                    <?php 
                        endwhile; 
                    ?>

                    <?php
                        else :
                            echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
                        endif;
                        wp_reset_postdata();
                    ?>  
                </ul>
   </div> <!-- #tab-content1 -->





   <div id="tab-content2" class="tab-content">
       <ul class="posts--group v-category-games">

                    <?php 
                        global $post; 
                        rewind_posts();
                        $query = new WP_Query(array(
                        'posts_per_page' => -1,
                        'cat' => 4,
                        's' => get_search_query(),
                        ));

                        if (have_posts()) : 
                            while ($query->have_posts()) : $query->the_post(); 

                    ?>

                     <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>       


                        <li>
                            <div class="post-item v-category-games v-with-image">
                                <div class="post-item--circle">
                                    <?php the_field('digest_number'); ?>
                                </div>

                                <div class="post-item--text no-margin">
                                    <a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    <span class="post-item--text--tagline">
                                        <span><a href="<?php the_permalink(); ?>"><p><?php the_field('short_description'); ?></p> </a></span>
                                        <span> </span>
                                    </span> 
                                </div>
                            </div>           
                        </li>

                    <?php 
                        endwhile; 
                    ?>

                    <?php
                        else :
                            echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
                        endif;
                        wp_reset_postdata();
                    ?>  

        </ul>
   </div> <!-- #tab-content2 -->

</div>

Related posts

1 comment

  1. You can use your if (have_posts()) : on each loop to determine if to display or not…

    if (have_posts()) :  
       //while loop
    
    else:
         echo '<h1>no posts found</h1>';
    
    endif;
    

Comments are closed.