如何在搜索结果中添加分页

2023-12-19

我的 search.php 页面如下所示:

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 循环内,它调用另一个名为内容搜索的页面,它看起来像这样?

<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-## -->

我现在刚刚注意到,当我搜索“Where”时,它会向我显示所有包含“Where”一词的帖子。

那么,如何为其添加分页呢?


将其添加到您的functions.php file:

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);
    }
  }
}

使用它,您实际上可以设置每页的帖子数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在搜索结果中添加分页 的相关文章

随机推荐