WP_Query Pagination on multiple-loop page breaks WP or doesn’t show up

The loop below is one of several on a Custom Page template, but it’s the only loop that I’m trying to implement Pagination with.

Once I have Pagination working for this one loop, I’ll be implementing InfiniteScroll which I’m sure will be way easier than installing it first…

Read More

I’ve read miles of pages but my brain just isn’t finding the right information to get this to work yet.

Designing themes is one thing, but engineering loops is still something I struggle with.

    <div class="thirdLeft">
            <?php
            global $paged;
            $curpage = $paged ? $paged : 1;
            $args = array(
                'post_type' => 'location',
                'orderby' => 'post_date',
                'posts_per_page' => 5,
                'paged' => $paged
            );
            $first_query = new WP_Query('cat=2,-20&showposts=18&offset=2');
            while($first_query->have_posts()) : $first_query->the_post();
            ?>

                <?php get_template_part( 'content', 'single' ); ?>  
            <?php endwhile; ?>
    </div>

UPDATE

After scouring the web, I found this great tut:
http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/

Formatting my loop above to work and get the pagination it deserves, an updated version looks like this:

(not much changed from the tut, aside from custom categories)

    <?php add_filter('post_limits', 'my_post_limit'); ?>
    <div class="thirdLeft">
            <?php
                global $myOffset;
                $myOffset = 2;
                $temp = $wp_query;
                $wp_query= null;
                $wp_query = new WP_Query();
                $wp_query->query('offset='.$myOffset.'&cat=2,-20'.'&showposts=18'.'&paged='.$paged);
            ?>
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    </div>
        <div class="longLoopNav">
          <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
          <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
        </div>
        <?php $wp_query = null; $wp_query = $temp;?>
        <?php remove_filter('post_limits', 'my_post_limit'); ?>

Now to figure out Infinite Scroll to load more posts into this loop with Ajax

Related posts

1 comment

  1. Why can’t you write custom pagination?

    function theme_get_next_posts_link($query, $label = null, $max_page = 0)    
        {       
            global $paged;      
            if(!$max_page) $max_page = $query->max_num_pages;       
            if(!$paged) $paged = 1;     
            $nextpage = intval($paged) + 1;     
            if(null === $label) $label = "Older Posts";     
            if(!is_single() && $nextpage <= $max_page):         
                $attr = apply_filters( 'next_posts_link_attributes', '' );          
                echo '<a class="page-l" href="' . next_posts( $max_page, false ) . "" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';     
            endif;  
        }       
    
        function theme_get_previous_posts_link($label = null)   
        {       
            global $paged;      
            if(null === $label) $label = "Newer Posts";     
            if(!is_single() && $paged > 1):         
                $attr = apply_filters( 'previous_posts_link_attributes', '' );          
                echo '<a class="page-r" href="' . previous_posts( false ) . "" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a>';     
            endif;  
        }
    

    Using:
    <?php theme_get_next_posts_link($first_query); ?> for Older Posts
    <?php theme_get_previous_posts_link(); ?> for Newer Posts

    In your code:

    <?php
        global $paged;
        $args = array(
            'post_type'=>'post',
            'post_status'=> 'publish',
            'paged'=> $paged,
            'ignore_sticky_posts'=> 1,
            'posts_per_page'=>10 // -1 - all
        );
        $fp_query = new WP_Query( $args );
        if ( $fp_query->have_posts() ):
        while ($fp_query->have_posts()) : $fp_query->the_post();
    ?>
        Posts
    <?php endwhile;?>
        <div class="longLoopNav">
            <div class="alignleft"><?php theme_get_next_posts_link($fp_query); ?></div>
            <div class="alignright"><?php theme_get_previous_posts_link(); ?></div>
        </div>
    <?php endif; ?>
    

Comments are closed.