如何在搜索结果中添加分页 [英] How to add pagination in search results
本文介绍了如何在搜索结果中添加分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的search.php页面如下:
My search.php page looks like this:
get_header(); ?>
<div class="search_result_page">
<section class="content-area">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
if ( have_posts() ) : ?>
<header class="results_heading">
<h6 class="rsults_title"><?php printf( esc_html__( 'Search Results for: %s', 'satsco' ), '<span class="search_query">' . get_search_query() . '</span>' ); ?></h6>
</header><!-- .page-header -->
<div id="result_search">
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'search' );
endwhile;?>
</div>
<?php
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</section><!-- #primary -->
</div>
<?php
get_footer();
在while循环中,它调用了另一个名为content-search的页面,看起来像这样吗?
Inside the while loop it calls another page called content-search which looks like this?
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
<?php if ( 'post' === get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
</article><!-- #post-## -->
现在我刚刚注意到,当我搜索哪里"时,它会向我显示所有带有哪里"一词的帖子.
I just noticed now that when I search for "Where", it shows me all the posts that have the word Where.
那么,如何向它添加分页?
So, how can I add pagination to it?
推荐答案
将此添加到您的functions.php
文件:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('paged', ( get_query_var('paged') ) ? get_query_var('paged') : 1 );
$query->set('posts_per_page',6);
}
}
}
通过它,您可以实际设置每页的帖子数.
With which, you can actually set the number of posts per page.
这篇关于如何在搜索结果中添加分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文