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!

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


Craig Jamieson
Craig Jamieson
19,585 Points

Custom Posts Pagination

I am having trouble adding pagination to my custom posts. Please can someone advise before I lose what little hair I have left.


    Template Name: Work Page


get_header(); ?>

<div id="content" class="clearfix">


$args = array( 
    'post_type' => 'work'

$the_query = new WP_Query( $args );

<h1><?php the_title(); ?></h1>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<section class="post-content clearfix">
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>

                        <?php endwhile; ?>  

                                <nav class="wp-prev-next">
                                        <li class="prev-link"><?php next_posts_link(__('&laquo; Older Entries')) ?></li>
                                        <li class="next-link"><?php previous_posts_link(__('Newer Entries &raquo;')) ?></li>

                        <?php else : ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

<?php get_footer(); ?>```

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

The WP document here: http://codex.wordpress.org/Function_Reference/next_posts_link states "Add the $max_pages parameter to the next_posts_link() function when querying the loop with WP_Query."

You need to pass to your query the paged variable to help build them, so your query will look something like

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 
    'post_type' => 'work',
    'paged'=> $paged
$the_query = new WP_Query( $args )

Than your pagination links can be like this

next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );

This is untested. But based on the WP codex this should work. Let me know.

Craig Jamieson
Craig Jamieson
19,585 Points

Thanks for your answer.

While it managed to get the 'Older Entries' link to display, unfortunately it directs my index.php file and not the pages within my work.php

Craig Jamieson
Craig Jamieson
19,585 Points

Thanks Zac, but it behaves just like the previous script, it goes to the second page and shows my index file. I haven't setup a 404 page yet, but I assume it would show that in place of the index file. It seems a couple of people have experienced the same problem, just working through their solutions to see which will work for me.

Anthony Moore
Anthony Moore
2,282 Points

If you have not re-saved your permalinks after adding in the custom post type you can try that and see if it resolves your issue.

Craig Jamieson
Craig Jamieson
19,585 Points

Gave that a try, but unfortunately it did not work either. Thanks for the suggestion though, was worth a try.