0

I made a fix that when the searchbar is empty, it returns 'No results', instead of all posts, but the pagination still shows up. Does anyone know a fix for that?

My code search.php:

<div class="wrapper" id="search-wrapper">
<div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">
    <div class="row">
        <main class="container" id="main">
            <?php if (have_posts() && strlen( trim(get_search_query()) ) != 0 ) : while (have_posts()) : the_post(); ?>
            <header class="page-header">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-12 text-center">
                            <div class="">
                                <h1>Zoekresultaten voor: 
                                    <?php   
                                    printf(
                                        /* translators: %s: query term */
                                        esc_html__( ' %s', 'understrap' ),
                                        '<span>' . get_search_query() . '</span>'
                                    );
                                    ?>
                                </h1>
                            </div>
                        </div>
                    </div>
            </header>
            <div>
                <h4>
                    <?php /* Start the Loop */ ?>
                    <?php get_template_part( 'loop-templates/content', 'search' ); ?>
                </h4>
            </div>
            <?php endwhile; else:?>
                <div class="container whitespace">
                    <div class="row">
                        <div class="col-lg-12 text-center">
                            <p class="display-4">Geen Resultaten..</p>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </main>

        <!-- The pagination component -->
        <div class="container">
            <div class="row mt-3">
                <?php understrap_pagination(); ?>
            </div>
        </div>
    </div>
</div>

My functions.php code:

    function SearchFilter($query) {
    // If 's' request variable is set but empty
    if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
        $query->is_search = true;
        $query->is_home = false;
    }
    return $query;}
add_filter('pre_get_posts','SearchFilter');
0

add this code in your search.php

<?php 
$show_pagination = true;
if($wp_query->post_count < 0) {
    $show_pagination = false;
}
?>

<?php if($show_pagination){ ?>
    <!-- The pagination component -->
            <div class="container">
                <div class="row mt-3">
                    <?php understrap_pagination(); ?>
                </div>
            </div>
<?php } ?>

So your search.php look like this :

<div class="wrapper" id="search-wrapper">
<div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">
    <div class="row">
        <main class="container" id="main">
            <?php if (have_posts() && strlen( trim(get_search_query()) ) != 0 ) : while (have_posts()) : the_post(); ?>
            <header class="page-header">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-12 text-center">
                            <div class="">
                                <h1>Zoekresultaten voor: 
                                    <?php   
                                    printf(
                                        /* translators: %s: query term */
                                        esc_html__( ' %s', 'understrap' ),
                                        '<span>' . get_search_query() . '</span>'
                                    );
                                    ?>
                                </h1>
                            </div>
                        </div>
                    </div>
            </header>
            <div>
                <h4>
                    <?php /* Start the Loop */ ?>
                    <?php get_template_part( 'loop-templates/content', 'search' ); ?>
                </h4>
            </div>
            <?php endwhile; else:?>
                <div class="container whitespace">
                    <div class="row">
                        <div class="col-lg-12 text-center">
                            <p class="display-4">Geen Resultaten..</p>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </main>

        <?php 
       $show_pagination = true;
       if($wp_query->post_count < 0) {
            $show_pagination = false;
       }
      ?>
     <?php if($show_pagination){ ?>
       <!-- The pagination component -->
        <div class="container">
            <div class="row mt-3">
                <?php understrap_pagination(); ?>
            </div>
        </div>
      <?php } ?>
    </div>
</div>

let me know if it helps you!

  • how does this answer the question? How does it work? – Tom J Nowell Jun 18 at 10:04
  • $wp_query->post_count object check how many search results number. if there is none then it false the flag $show_pagination. so when $show_pagination is false, the pagination will not display. – Chetan Vaghela Jun 18 at 10:12
  • can you update the answer with that? – Tom J Nowell Jun 18 at 10:13
  • i have updated answer with that. – Chetan Vaghela Jun 18 at 10:17
  • 2
    This seems like overkill. Why can't you just check have_posts()? – Jacob Peattie Jun 18 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.