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

jack hamilton
jack hamilton
5,609 Points

Adding pagination.

Hey could anyone share with me how I would set up pagination using the wp_query method that zac uses?

I had a look at some others and coundnt get it to work, the closest I got was this one I put on stackoverflow alas it still not work.

If anyone would like to see the code I am trying to attempt this one it is here on pastebin

THANK YOU :)

7 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Jack!

The posts_per_page parameter will limit how many posts appear, but it's the paged parameter that will enable/disable pagination.

Try this and let me know if it works:

<?php
$args = array (
        'post_type' => 'news',
        'posts_per_page' => 2,
        'paged' => $paged
);

$the_query = new WP_Query( $args );
?>   
jack hamilton
jack hamilton
5,609 Points

Thank you for your time Zac,

I did follow a few changes and found this sort of works, pastebin - although the problem is when it goes to page '2' it defaults to the index page, image, is this what it is meant to do?

Do I need to set up a new page for this sort of thing and if so do you happen to know if there are any good examples?

Zac Gordon
Zac Gordon
Treehouse Guest Teacher

Hmm, not sure why that is happening. It should use the same template you have that code in when it loads the next page.

If you disable the plugin for a moment and just use this code by itself posts_nav_link() does it work?

jack hamilton
jack hamilton
5,609 Points

No sorry that does not work. What I can do though is I will soon have a copy of my server up on raidhost and I can send you a login maybe so you can actually see what is happening, as I know how hard it is to "guess" at what is wrong if you cannot see all the code.

Thank you so much for all the help, I'll be here if you do have a lightbulb idea of why it may not be working though!

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

How do you have your static homepage / blog post page setup?

jack hamilton
jack hamilton
5,609 Points

Those are the reading settings yes? I currently have them like this Although I have not really used this since going through your videos as I cannot remember when I set it.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hmm, strange. So you're on your news page and you see everything properly. Then you click on next page and it goes to a different URL and uses a different template or does it just switch to using a different template but the correct URL?

jack hamilton
jack hamilton
5,609 Points

Yes only when using this code from here do I get the pagination to show up.

Once it does show up it then redirects to a different url

Matt Campbell
Matt Campbell
9,767 Points

The codex has a code snippet that provides reliable pagination and gives you a PHP call to enable pagination where and when you want it.

Here's the link - http://codex.wordpress.org/Function_Reference/paginate_links

It's what I use when I want to paginate.

jack hamilton
jack hamilton
5,609 Points

Yeah I allready tried that in the link above.Thanks though.

The problem is the links come up but when I link on them it goes to the permalink "page 2" athough nothing is shown it just shows the index.php page.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

What is the URL you start on and what is the URL it redirects to?

jack hamilton
jack hamilton
5,609 Points

Werid quick update I attempted the methods on my video page and it worked! So now to try and see what is going wrong on the news page! I'll update here later.

So I worked out that it is now to do with permalinks. When I used the default setting it works but with permalinks it breaks. Any ideas why?

I did try the reduce amount of blog posts but that didn't seem to work.

So after pretty much giving up I think I got it working but changing the name of the news post type and then switching it back now its working and will stay like that fingers crossed!

Adam Soucie
PLUS
Adam Soucie
Courses Plus Student 8,710 Points

I've run into this issue before. Usually I just have to save my permalinks after I make a change, and everything is fine again.

Darn permalinks screw everything up.