Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Jennifer Hinkle
Jennifer Hinkle
8,365 Points

Older Posts

Hello, I've been working through the How to Build a Wordpress Theme track. I have a question about the blog posts. If I have more than 10 posts, Wordpress automatically only shows 10 of them. However, I cannot figure out how to add a button saying "View more posts" or "older posts" or anything along that functionality. My questions: How do I add this button? If I have multiple pages of posts, can I number the pages so the user doesn't have to keep scrolling down and clicking "older posts"? Can I change the number of posts shown? Instead of 10, show only 5? Lastly, once you click into a single-post, how do I add the buttons saying "next post" and "previous post"?

Sorry for the multi-part question. I've been googling this, but I don't think I'm using the right keywords because I can't seem to figure it out. Any help or direction would be greatly appreciated!

Thanks! Jenn

2 Answers

Kevin Korte
Kevin Korte
28,109 Points

Let's break this down by your questions

Found here: https://codex.wordpress.org/Pagination

This is what a basic page template with a loop might look like. Notice the pagination functions after the loop. This will add the buttons that say older posts and newer posts, which that text can be changed.

<?php if ( have_posts() ) : ?>

<!-- Add the pagination functions here. -->

<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post();  ?>

<!-- the rest of your theme's main loop -->

<?php endwhile; ?>
<!-- End of the main loop -->

<!-- Add the pagination functions here. -->

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

This should be fine to use on a loop of posts, or on a single post. WP should be smart enough to figure it out. Lots of options on this document.

If used the last option on this page http://wp.tutsplus.com/tutorials/wordpress-pagination-a-primer/ to successfully add numbers to the pagination like you asked.

And finally, you can change how many posts per page in your settings tab in your wp admin dashboard. I think it's in settings >> reading and there is a menu item on that page that says something like "Posts per page" and it will say 10. You can change that number to whatever you want.

Jennifer Hinkle
Jennifer Hinkle
8,365 Points

Thanks so much for the quick response! I did this, and it worked perfectly on my blog page. However, I assumed it would work the same on a custom post type page. Is this not the case? Do you know what I should be doing differently in a custom post type page? I googled it but a lot of people are say I need to do something with WP_posts as opposed to WP_Query. But I've gotten the impression from Zac that I should stick with the Query. So again, maybe I'm googling the wrong terms? (now I know pagination- so thanks for cluing me into that one :)

Thanks again for your help! I super appreciate it!

(sorry, meant that to be a comment, not an answer)