如何让分页在 WordPress 中为 get_posts() 工作?

2022-01-04 00:00:00 php wordpress pagination

我在一个 WordPress 网站上工作,我创建了一个页面模板,该模板按类别 slug 显示帖子.为此,我为页面创建了一个字段 WP_Catid,并将其设置为等于我要从中显示帖子的类别 slug.但是,我只希望每页显示五个帖子,并在这些帖子底部显示分页链接.如何让分页链接正确显示?

我的代码如下:

<div id="content" role="main"><?php$btpgid=get_queried_object_id();$btmetanm=get_post_meta( $btpgid, 'WP_Catid','true' );$paged = (get_query_var('paged')) ?get_query_var('paged') : 1;$args = array( 'posts_per_page' => 5,'category_name' =>$btmetanm,'分页' =>$分页,'post_type' =>'邮政' );$myposts = get_posts( $args );foreach ( $myposts as $post ) : setup_postdata( $post );echo "<div style='border:2px 凹槽黑色; margin-bottom:5px;'><h3 class='btposth'>";标题();echo "</h3><div class='btpostdiv'>";内容();echo "</div></div>";Endforeach;next_posts_link('旧条目');//不显示previous_posts_link('较新的条目&raquo;');//不显示wp_reset_postdata();?></div><!-- #content --></div><!-- #container -->

解决方案

这个好又短,不要使用 get_posts 如果您需要分页查询.如果您打算使用不需要分页的自定义查询,get_posts 非常有效,但是当您需要引入分页时,它确实变得非常复杂.

我认为这里最简单和最合适的是使用 WP_Query 来构建您的自定义查询,即如果您不能使用 pre_get_posts 更改主查询,以从主查询中获得所需的输出.

我确实认为 next_posts_link()previous_posts_link() 最好与自定义查询一起使用,即与 WP_Query.但是,您必须记住在使用自定义查询时设置 $max_pages 参数,否则您的分页会中断

经过一些细微的调整,您的查询应该如下所示

<div id="content" role="main"><?php$btpgid=get_queried_object_id();$btmetanm=get_post_meta( $btpgid, 'WP_Catid','true' );$paged = (get_query_var('paged')) ?get_query_var('paged') : 1;$args = array( 'posts_per_page' => 5, 'category_name' => $btmetanm,'分页' =>$paged,'post_type' =>'邮政' );$postslist = new WP_Query( $args);如果( $postslist->have_posts() ):while ( $postslist->have_posts() ) : $postslist->the_post();echo "<div style='border:2px 凹槽黑色; margin-bottom:5px;'><h3 class='btposth'>";标题();echo "</h3><div class='btpostdiv'>";内容();echo "</div></div>";终了;next_posts_link( '旧条目', $postslist->max_num_pages );previous_posts_link('下一个条目&raquo;');wp_reset_postdata();万一;?></div><!-- #content --></div><!-- #container -->

I'm working on a WordPress site and I've created a page template that displays posts by a category slug. To do this, I create a field for the page, WP_Catid, and set it equal to the category slug I want to display posts from. However, I only want five posts to show up per page with pagination links at the bottom of those posts. How do I get the pagination links to display properly?

My code is as follows:

<div id="container">
  <div id="content" role="main">
    <?php
      $btpgid=get_queried_object_id();
      $btmetanm=get_post_meta( $btpgid, 'WP_Catid','true' );
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

      $args = array( 'posts_per_page' => 5,
                     'category_name' => $btmetanm,
                     'paged' => $paged,
                     'post_type' => 'post' );

      $myposts = get_posts( $args );
      foreach ( $myposts as $post ) : setup_postdata( $post ); 
        echo "<div style='border:2px groove black; margin-bottom:5px;'><h3 class='btposth'>";
        the_title(); 
        echo "</h3><div class='btpostdiv'>";
        the_content();
        echo "</div></div>";
      endforeach; 
      next_posts_link( 'Older Entries'); //not displaying
      previous_posts_link('Newer Entries &raquo;'); //not displaying
      wp_reset_postdata();
    ?>
  </div><!-- #content -->
</div><!-- #container -->

解决方案

The sweet and short of this, don't use get_posts if you need paginated queries. get_posts works perfectly if you are going to use a custom query that doesn't need pagination, but it really becomes a big complicated mess when you need to introduce pagination.

I think the easiest and most appropriate here is to make use of WP_Query to construct your custom query, that is, if you can't use pre_get_posts to alter the main query to get your desired output from the main query.

I do think that next_posts_link() and previous_posts_link() is better to use with a custom query, ie with WP_Query. You must just remember however to set the $max_pages parameter when you make use of a custom query, otherwise your pagination will break

With a few minor tweaks, your query should look like this

<div id="container">
<div id="content" role="main">
<?php
$btpgid=get_queried_object_id();
$btmetanm=get_post_meta( $btpgid, 'WP_Catid','true' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 'posts_per_page' => 5, 'category_name' => $btmetanm,
'paged' => $paged,'post_type' => 'post' );
    $postslist = new WP_Query( $args );

    if ( $postslist->have_posts() ) :
        while ( $postslist->have_posts() ) : $postslist->the_post(); 


             echo "<div style='border:2px groove black; margin-bottom:5px;'><h3 class='btposth'>";
                 the_title(); 
             echo "</h3><div class='btpostdiv'>";
                 the_content();
             echo "</div></div>";

         endwhile;  

             next_posts_link( 'Older Entries', $postslist->max_num_pages );
             previous_posts_link( 'Next Entries &raquo;' ); 
        wp_reset_postdata();
    endif;
    ?>

</div><!-- #content -->
</div><!-- #container -->

相关文章