0

Possible Duplicate:
Pagination not working with custom loop

So I have a post type called portfolio, but I can't get it to paginate. I'd also like it to paginate on single pages so each item has a prev/next link at the bottom, but this won't work either.

I try adding this on the single page and the links just don't appear.

I've been banging my head against a brick wall for days now trying to find a solution for this and nowhere online can I find one.

<div class="next-posts"><?php next_posts_link('Older Posts') ?></div>
            <div class="prev-posts"><?php previous_posts_link('Newer Posts') ?></div>




<?php
                                 $portfolio_query = new WP_Query(array(
                                'post_type' => 'portfolio',
                                'showposts' => -1
                                ) );
                           ?>


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

                            <?php global $more; $more = 0; ?>

                         <div class="portthumbwrap">

                         <a href="<?php the_permalink() ?>">
                            <div class="portmetacontainer">
                                <div class="link-text">
                                    <h2><?php the_title(); ?></h2>

                                    <ul>
                                     <?php
                                        $posttags = get_the_tags();
                                        if ($posttags) {
                                          foreach($posttags as $tag) {
                                            echo '<li>' .$tag->name. '</li>'; 
                                          }
                                        }
                                        ?>

                                    </ul>   

                                </div> <!-- end div link-text -->

                            </div> <!-- end div postmetacontainer -->
                         </a>

                           <div class="figure">

                                <?php the_post_thumbnail(); ?>

                           </div>


                         </div> <!-- end div portthumbwrap -->











                           <div class="clearfix"></div>
                           <?php endwhile; wp_reset_query(); ?>
3
  • 1
    why do you create a new query for your portfolio items? what template are you using this in? what happens if you just run the default loop? for single views you want next_post_link, note that it's singular post, not plural posts.
    – Milo
    Jan 29, 2013 at 16:21
  • It says 'posts' here. codex.wordpress.org/Function_Reference/previous_posts_link Why not create a new query for the portfolio posts? I'm not being sarcastic or anything, I really don't know, the WP codex seems geared towards people that are experts in php. And the template is one I made.
    – andy
    Jan 29, 2013 at 19:09
  • Please search for related questions before posting. Jan 29, 2013 at 19:36

2 Answers 2

0

I believe if posts_per_page is set to -1 you're showing all posts without pagination. Try setting it to 5 for example.

1
0

The problem with your custom query is that you pass no pagination parameters, and it has no relationship to the default query variables that pagination functions operate with.

When you register your portfolio post type, set has_archive to true and rewrite slug to the URL you want your portfolio items to appear at:

'rewrite' => array( 'slug' => 'portfolio' )

then use the default loop in your template:

while( have_posts() ):
    the_post();
    the_title();
    // etc...
endwhile;

and the pagination functions will just work without you having to do anything else.

If you want to customize the template from other post archives, name your template archive-portfolio.php where portfolio is the name you've registered your post type as. See the Template Hierarchy for more info.

next_posts_link (plural) is for an archive page, next_post_link (singular) is the next single post after the current single post being viewed.

1
  • I just changed next_posts_link to post (singular) and added 'archive' to the 'supports' array. Now it paginates just fine on the single page. Is that 'right' in technical sense?
    – andy
    Jan 29, 2013 at 23:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.