我做了一个修复,当搜索栏为空时,它会返回“无结果”,而不是所有帖子,但分页仍会显示出来。有人知道解决方法吗?
我的代码搜索。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\');
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>
如果对你有帮助,请告诉我!