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...

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">
            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; ?>


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">
                global $myOffset;
                $myOffset = 2;
                $temp = $wp_query;
                $wp_query= null;
                $wp_query = new WP_Query();
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div class="longLoopNav">
          <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
          <div class="alignright"><?php next_posts_link('More &raquo;') ?></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


1 Answer 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>';     

    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>';     

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

In your code:

    global $paged;
    $args = array(
        '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();
<?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>
<?php endif; ?>
  • I'm still relatively new to working with custom queries, but this looks really thorough. Would you be able to guide me a little with a run through of what you're doing here?
    – canacast
    Commented Aug 2, 2013 at 7:22
  • I added example of using in my post.
    – Vladimir
    Commented Aug 2, 2013 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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