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

Kellie Kowalski
Kellie Kowalski
27,641 Points

Simple Pagination on Custom Post Template Page

I've been building a theme from scratch by following the How to Build a Wordpress Theme course. I'm able to get pagination to work on the home.php page and absolutely nowhere else. I've tried using the code from this forum, the Codex, and CSS-Tricks and it's either done nothing or broken the page.

Here's what I've got on my template page. Any ideas?

<?php 

/*

    Template Name: Workshops Page

*/


get_header(); ?>

<div id="page-image">
    <div class="wrapper">
        <h1 id="page-title"><?php the_title(); ?></h1>
    </div>
</div>

<?php

    $args = array(
        'post_type' => 'workshops',
        'posts_per_page' => 6,
            'paged' => $paged
    );

    $the_query = new WP_Query( $args );

?>

<div class="wrapper">
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <?php get_template_part( 'content', 'post' ); ?>

    <?php endwhile; else: ?>

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

    <?php endif; ?>

    <div class="clearfix"></div>

    <div>
        <div class="right"><?php previous_posts_link('Newer &raquo;') ?></div>
        <div class="left"><?php next_posts_link('&laquo; Older') ?></div>
    </div>


</div>

<?php get_footer(); ?>

2 Answers

The Codex says that next_posts_link() and previous_posts_link() will not work on static pages. However, I'm pretty sure that, if you call those functions within the if-block of the custom Loop, they'll output correctly.

<?php

$args = array(
    'post_type' => 'workshops',
    'posts_per_page' => get_option( 'posts_per_page' ), // Lets posts per page be configured in the admin area
    'paged' => $paged,
);

$the_query = new WP_Query( $args );

?>

<!-- HTML here -->

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

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

        <!-- Loop HTML here -->

    <?php endwhile; ?>

    <?php next_posts_link( __( 'Older entries' ), $the_query->max_num_pages ); ?>

    <?php previous_posts_link( __( 'Newer entries' ) ); ?>

<?php endif; ?>

Not completely sure about that, but I think it's worth a shot.

Kellie Kowalski
Kellie Kowalski
27,641 Points

Thanks, but I'm afraid that didn't do the trick either. The get option( 'posts_per_page' ) bit is super handy though; I hadn't come across that yet.