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 »'); //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 usepre_get_posts
to alter the main query to get your desired output from the main query.I do think that
next_posts_link()
andprevious_posts_link()
is better to use with a custom query, ie withWP_Query
. You must just remember however to set the$max_pages
parameter when you make use of a custom query, otherwise your pagination will breakWith a few minor tweaks, your query should look like this
Pieter Goosen’s answer is completely correct, and his suggestion to use
WP_Query
instead makes the most sense. However, I stumbled across this question whilst looking for pagination withget_posts
outside of the loop, so I thought this might also be a useful alternative for anyone else:get_posts
has a direct property calledoffset
which achieves pretty much the same thing aspaged
inWP_Query
; but wherepaged
refers to pagination (e.g. 1, 2, 3),offset
is the actual number of posts you want to offset your query by (e.g. 5, 10, 15). With a little maths –numberToShow * pageNumber
– you can get the correct offset easily:The initial
paged
value in this example is0
rather than1
because, when multiplying theposts_per_page
, you want the initial offset to be0
rather than5
.This can be most handy if you want a little more granular control rather than straightforward pagination, but should work just as well in combination with the loop in the accepted answer.
Try to change your $args:
And just after loop put this:
I will not tell you that using get_posts() is the right thing to do…but here is some basic pagination code I setup using get_posts().
EDIT: As Pieter pointed out, this isn’t meant to be used in production code. But just something I was playing around with anyway to see if I could get pagination working with get_posts(). If you are in a production environment you wouldn’t want to use this.
I hope someone gets some use out of this 🙂
EDIT: What the heck! Going to do something the wrong way…might as well do it right! Here is some LESS as well (without any mixins).
WordPress Pagination Code for Posts (NEW)
Copy and paste this piece of code and enjoy that. 🙂