1

I have a custom loop and I'm trying to break it up into several pages but I'm not getting the next and previous links to show. Here is my code:

                <?php

                    if ( get_query_var( 'paged' ) )
                        // On a paged page.
                        $paged = get_query_var( 'paged' );

                    else if ( get_query_var( 'page' ) )
                        // On a "static" page.
                        $paged = get_query_var( 'page' );

                    else
                        // Default.
                        $paged = 1;

                    $args = array(
                        'post_type'      => 'property',
                        'orderby'        => 'meta_value',
                        'meta_key'       => 'random_775',
                        'order'          => 'ASC',
                        'paged'          => $paged,
                        'posts_per_page' => 10,
                    );
                ?>

                <?php $count = 0;

                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <?php next_posts_link('Next' ); ?>
                        <?php previous_posts_link('Back' ); ?>

I'm not sure what I'm doing wrong or is someone can shed some light on it.

Thanks in advance!

0

2 Answers 2

2

Page link functions depend on the value in the $paged global variable. So, we need to update that variable as well as the parameter in the custom query.

If we are on a "static" front page, the 'paged' query variable should be undefined and the 'page' query variable may be defined.

If neither query variable is defined we are probably on page 1.

// Only needed if this code is called via a function.
global $paged;

if ( get_query_var( 'paged' ) )
    // On a paged page.
    $paged = get_query_var( 'paged' );

else if ( get_query_var( 'page' ) )
    // On a "static" page.
    $paged = get_query_var( 'page' );

else
    // Default.
    $paged = 1;

$args = array(
    'post_type'      => 'property',
    'orderby'        => 'meta_value',
    'meta_key'       => 'random_775',
    'order'          => 'ASC',
    'paged'          => $paged,
    'posts_per_page' => 10,
);

Code adapted from the Pagination WordPress Codex page.

EDIT:

$count = 0;

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
    $loop->the_post();

    next_posts_link( 'Next', $loop->max_pages );
    previous_posts_link( 'Back' );
4
  • I've edited my code based off of your suggestions but I'm still not showing the pagination links. I did do print_r($paged) and it displays 1 if that helps. Thanks for your help! Commented Sep 24, 2013 at 20:13
  • Try adding the max pages parameter to the next_posts_link() call. See the edit above. Commented Sep 25, 2013 at 2:02
  • I tried your changes but it still is not showing. Thanks for the help anyway. Commented Sep 25, 2013 at 2:44
  • The function reference states that: "This function will not work (fail silently) if mysql.trace_mode is enabled in your php.ini." Have you verified that condition? Commented Sep 25, 2013 at 3:00
0

For simplicity, I strongly recommend using the WP PageNavi plugin. It's beautiful, and super easy to implement. Below the loop, you just add wp_pagenavi(); and you're done.

The code you have in right now is for next/previous post, which would be used on a single post page. What you're looking for is posts_nav_link(), also explained here.

1
  • I've used wp PageNavi in the past but I was hoping to use infinite scroll once I got pagination sorted out. I've actually got wp PageNavi working on the site but I can't get infinite scroll working with it so I'm not sure if that is an option for me. I still can't get it to work with posts_nav_link() though. Commented Sep 24, 2013 at 21:10

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