dorich
dorich

Reputation: 1089

How to create navigation for custom post type archive page

I have a template that displays the titles of a custom post type. I have approximately 60 posts in the group. A page is set to display 10 posts per page. This yields six pages to display all the titles but I'm having trouble getting the navigation (page to page) to function.

In the code below I get the first set of titles returned but clicking on the 'previous' 'next' links only displays the same set of titles. Searching around I can't find any definitive solutions on how to format a template to navigate between groups of titles for a custom post type. I did find that CPTs need to have the max_num_pages defined. So if I insert a value in place of $max_num_pages I do get a result where you can click from page to page but once you reach the end of the list you click onto an empty page.

    <h2>Glossary</h2>
     <?php $loop = new WP_Query( array( 'post_type' => 'glossary', 'posts_per_page' => 10, 'paged='.$paged, 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
     <?php $max_num_pages=$loop->max_num_pages ?>
     <p>Maximum Number of Pages Value: <?php echo $max_num_pages;// This line for debug purposes only?></p>
                <?php if ( have_posts() ) : ?>
                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
        </div>
                <?php endwhile; ?>
                    <div class="navigation">

<div class="alignleft"><?php next_posts_link('Previous entries',$max_num_pages) ?></div>
<div class="alignright"><?php previous_posts_link('Next entries',$max_num_pages) ?></div>

etc..

What code do you use to allow users to click from one group of title to another?

Thanks

Upvotes: 2

Views: 4597

Answers (1)

Ryan
Ryan

Reputation: 2503

Try adding the following before your WP_Query:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

Upvotes: 1

Related Questions