如何在WordPress中为get_posts()使用分页? [英] How do I get pagination to work for get_posts() in WordPress?
问题描述
我正在WordPress网站上工作,并且创建了一个页面模板,该页面模板按类别条目显示帖子.为此,我为页面创建了一个字段WP_Catid,并将其设置为要显示其帖子的类别条目.但是,我只希望每页显示五个帖子,并在这些帖子的底部带有分页链接.如何使分页链接正确显示?
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?
我的代码如下:
<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 »'); //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.
我认为这里最简单,最合适的方法是利用 WP_Query
进行构造您的自定义查询,即,如果您不能使用 pre_get_posts
更改主查询,以从主查询中获得所需的输出.
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.
我确实认为 next_posts_link()
和
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 »' );
wp_reset_postdata();
endif;
?>
</div><!-- #content -->
</div><!-- #container -->
这篇关于如何在WordPress中为get_posts()使用分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!