Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBob Sutherton
20,160 PointsPaginating a custom post type list in WordPress?
The WordPress site I am building has a custom post type for each page. On each page, is a list of the posts that correspond to the custom post type for that page.
I am trying to get pagination for each of these pages and their custom post type posts lists.
I have tried using the standard approaches like:
posts_nav_link();
next_posts_link();
previous_posts_link();
All I can figure is that these are not compatible when you are looping custom post types. What do you think?
5 Answers
Kevin Korte
28,149 PointsI've always had good luck following the below article to paginate custom post types. It is a bit more tricky.
http://codex.wordpress.org/Function_Reference/paginate_links
Bob Sutherton
20,160 PointsI haven't been able to do anything with it so far.
Bob Sutherton
20,160 PointsUPDATE: I was able to get the buttons to appear at the bottom of the page but they only show the same page and posts over and over again.
<?php
$args = array (
'post_type' => 'bhagavan'
);
$the_query = new WP_Query( $args );
?>
<?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="wrapper">
<div class="excerpt">
<div id="featz"><img src="<?php the_field('featured_image'); ?>" class="feat-img"></div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
</div>
</div>
<?php endwhile; ?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
<?php else: ?>
<p>There are no posts or pages here.</p>
<?php endif; ?>
Kevin Korte
28,149 PointsHow many posts are in that loop, and how many posts are set to display per page in your WP settings?
Bob Sutherton
20,160 PointsHey Kevin, my reading settings are at 10 posts per page. 5 pages are showing up on the actual page, and there are I think 23 custom posts of that type.
Kostas Oreopoulos
15,184 PointsIf anyone still has any problems with pagination a suberb article on it can be found here
basically , next_posts_links do not work with static pages (that get content through page templates). You have to use get_next_posts_links.
The article explains it in great detail and even create a great pagination function for everything.
Bob Sutherton
20,160 PointsAwesome, thanks for sharing it!
Bob Sutherton
20,160 PointsBob Sutherton
20,160 PointsThanks Kevin. I'll give it the old college try.