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 trial

WordPress

Custom Post Type Pagination

I've successfully incorporated custom post types into a WordPress site, but I'm having difficulty figuring out how to show pagination. I figured out how to get it to show a certain number of posts, and that works, but I want to offer a "previous posts" link to see the posts beyond that limit (and then add a "newer posts" type link to go back to the newer ones).

Can you direct me to a specific example to build on what we learn in the Treehouse lesson? (I've tried what's on the Codex, but it's not working/I need more info.)

Thanks.

3 Answers

Jennifer Hinkle
Jennifer Hinkle
8,365 Points

Hello again,

So this is a problem that I've been trying to figure out for a couple of days now and have been getting pretty frustrated. I tried using various plugins, wp-pagenavi being the one that almost worked. I selected the option to "Always show page navigation" for debugging. That showed I had pagenavi working, but it was linking incorrectly. On one custom post page I had it displaying only 3 posts/ page (while there were 8 in total). And for some reason, it made it seem as if there was only the one page of posts, and there was no button to click to view more. Not sure how to address this problem, I took back to google and ended up on CSS-Tricks again. I just started reading all of the comments because I couldn't think of anything else and I came across this:

<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=snippets'.'&paged='.$paged);
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php $wp_query = null;
$wp_query = $temp;
?>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>

Where it says type=snippets I changed snippets to my custom post type name. Also with this, you can change the number of posts shown on the page (over ruling whatever is set in your Wordpress reading settings) where it says: showposts=6& ... change the 6 to the number of posts you want to display. Honestly, I don't really understand why, but this code worked perfectly for me on one of my custom post type pages. Not working on the other, I found out for some reason your page-slug has to be different than the custom post type name. If they are the same, it doesn't work. Again, I'm not exactly sure why. But that's what solved all of this for me. Just wanted to post my answers here in case anyone else comes across this needing help!

Good luck, Jenn

Simon Tucker
Simon Tucker
9,113 Points

Thank you Jenn. I have been rattling my brains for days figuring why my pagination will not display then I realised my query name was different. Realising I had query instead of wp_query. Brilliant I thought, but then my posts would display the same on all pages until I saw this $wp_query->query('showposts=6&post_type=snippets'.'&paged='.$paged);

Thanks very much ;-)

Jennifer Hinkle
Jennifer Hinkle
8,365 Points

Hi Bonnie,

I was wondering, did you ever figure this problem out? Because I'm having the same issue. I tried the code that Zac linked above, but it didn't work. For one of my custom post types, the "older posts" link appeared, but when I clicked it, no posts showed up- all content disappeared other the header and footer. (Even though the url went to /page/2). Then for a different custom post type, the link didn't even appear at all. I was just wondering if that code worked for you, and if not, if you've found a different solution.

Thanks, Jenn