Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm unable to get my 'next_posts_link()' and 'previous_posts_link()' to display. So far I've tried the standard first debug by deactivating all plugins and clearing the cache, I've also tried removing the optional parameters from the call. And I've also tried using wp-pagenavi, which does display but oddly only when I set my Reading options to show 1 post per page and even then it only shows 2 page links. The site I'm having the problem with is pixelsandtea.com and here's the code on my home template page.

<?php get_header(); ?>
<div id="main-inner">
<ul class="mcol" id="mcol">
        <?php
            $args = array( 'post_type' => array( 'post', 'portfolio', 'news' ) );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
        ?>
        <li class="article preview" id="post-<?php the_ID(); ?>">
            <h2 class="articleTitle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
            <?php $content = $post->post_content;
            $searchimages = '~<img [^>]* />~';
            preg_match_all( $searchimages, $content, $pics );
            $iNumberOfPics = count($pics[0]);
            if ( $iNumberOfPics > 0 ) { ?>
                <a href="<?php the_permalink(); ?>" class="thumb" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&w=279&zc=1" alt="<?php the_title(); ?>" /></a><?php
            }
            the_excerpt(); ?>
            <div class="postmetadata">
                Posted: <?php the_time(__('l, jS F, Y')) ?>&nbsp;&#721;&nbsp;<?php printf(__('Filed under: %s'), get_the_category_list(', ')); ?>&nbsp;&#721;&nbsp;<?php the_tags(); ?><br />
                <?php comments_popup_link(__('No commentaries for this post yet. You could be the first!'), __('1 Comment'), __('% Comments'), '', __('Comments are closed') ); ?><?php edit_post_link(__('Edit this entry'), '&nbsp;&#721;&nbsp;', ''); ?>                         
            </div>
        </li>
        <?php endwhile; ?>
        </ul>
        <div class="clear"></div> 
        <div id="nav">
            <div id="navleft"><?php next_posts_link() ?><div class='clear' style='clear:left;'></div></div>
            <div id="navright"><?php previous_posts_link() ?><div class='clear' style='clear:right;'></div></div>
        </div>
</div>
<?php get_footer(); ?>

Thank's all!

share|improve this question
    
possible duplicate of posts_nav_link() not displaying anything –  Chip Bennett Oct 30 '11 at 2:32
add comment

2 Answers

You need to do a little "hack" to get pagination to work for your custom loop.

After you define $loop, do the following:

<?php
// globalize $wp_query
global $wp_query;
// copy $wp_query into a temporary variable
$temp_wp_query = $wp_query;
// nullify $wp_query
$wp_query = null;
// move $loop into $wp_query
$wp_query = $loop;
?>

At this point, your posts_nav_link() should work as expected.

Now, after the loop, swap the original object back into $wp_query, so that everything else on the page that is query-dependent will work properly:

<?php
// restore original $wp_query
$wp_query = $temp_wp_query;
?>
share|improve this answer
    
Or you could just call query_posts( $args ); directly and at the end call wp_reset_query();. –  scribu Oct 30 '11 at 8:56
    
@ChipBennett - I've got the pagination links working now, only the loop is repeating the same posts with every page. To demonstrate the problem I have 12 posts which should be in this loop and I've set my reading options to show 5 posts per page, but they're all the same posts!... Here's a link to my updated code pastebin.com/kj5exnrH –  Ben Oct 30 '11 at 11:25
add comment

Try replacing those next/prev tags with this:

<?php next_posts_link('&laquo; Older Entries') ?>
<?php previous_posts_link('Newer Entries &raquo;') ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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