如果搜索返回为空,则删除分页

时间:2019-06-18 作者:Jurre N

我做了一个修复,当搜索栏为空时,它会返回“无结果”,而不是所有帖子,但分页仍会显示出来。有人知道解决方法吗?

我的代码搜索。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>

我的职能。php代码:

    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\');

1 个回复
SO网友:Chetan Vaghela

在搜索中添加此代码。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 } ?>
所以你的搜索。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>

        <?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>
如果对你有帮助,请告诉我!

相关推荐

Woocommerce budget search

我有woo commerce设置。现在的要求就像说,如果我搜索预算,比如说5000,它应该显示总价格为5000的产品(例如一个价格为3000的产品和一个2000奖的产品)。我们该怎么做,请帮忙。