0

I've about 12 records and that means I should have 4 pages. My best attempt so far at getting pagination to work is this, and I'd really appreciate if someone could tell me where exactly am I going wrong?

    <div id="main-content">
    <div class="container">

        <?php

            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            $args = array(
                'post_type' => 'job',
                'posts_per_page' => 3,
                'paged' => $paged
            );
            $query = new WP_Query( $args );
            while ( $query->have_posts()): $query->the_post();
                <h1> <?php the_title(); ?> </h1>
                <p> <?php  the_excerpt(); ?> </p>
                <?php
            endwhile; ?>

    </div> <!-- .container -->
</div> <!-- #main-content -->

<!-- Start Navigation Here -->
<?php
    global $wp_query;

    $total_pages    =   $wp_query->max_num_pages;

    if ($total_pages > 1) {

        $current_page = max( 1, get_query_var('paged'));
        echo '<div class="page_nav">';
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text' => 'Prev',
            'next_text' => 'Next'
        ));
        echo '</div>';
    }
?>

<?php wp_reset_postdata(); ?>

UPDATED CODE:

Okay this is much simplified, but this continues to show 5 pages irrespective of how many 'posts_per_page' count I set. With total of 24 records, I am expecting only 2 pages (aka max_num_pages), but I keep getting '5'. Here's my simplified, latest code:

        //Generate the loop here

        //Prepare arguments for WP_QUERY
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
        $args   = array(

            'posts_per_page'    => 12,
            'post_type'         => 'job',
            'paged'             => $paged

        );

    $query  = new WP_Query( $args );

    if ($query->have_posts()) {

        while ( $query->have_posts()) {
            $query->the_post();
            ?> <li><?php the_title(); ?></li> <?php
        }
    } else {
        echo "<h2>No Jobs Found</h2>";
    }
    // Pagination begins here
    $paginateArgs = array(
        'base'  => '%_%',
        'format' => '?paged=%#%',
        'current'   => $paged
    );
    echo paginate_links( $paginateArgs );
    wp_reset_postdata();
    ?>
  • Okay this is much simplified, but this continues to show 5 pages irrespective of how many 'posts_per_page' count I set. With total of 24 records, I am expecting only 2 pages (aka max_num_pages), but I keep getting '5'. Here's my simplified, latest code: [ADDED IT TO THE MAIN POST ABOVE] – TheBigK Oct 13 '16 at 6:55
0

Maybe, for your pagination you are using the global WP var $wp_query, and in the posts_per_page are the global on WP settings.

I think you would use your $query var for paginate too.

  • Thank you fcastillo, I did some further investigation and found out that the 'max_num_pages' is incorrect. I've no clue why it does so. I'll post code in some time. – TheBigK Oct 12 '16 at 15:56
  • Maybe beacuse your max_num_pages is getting from WP global var $wp_query and not your own var $query – fcastillo Oct 12 '16 at 16:31
0

Turns out that adding $big = 999999999 and then changing 'base' in $paginateArgs did the trick. I still have no clue why it works; but it does. Picked up the code directly from CODEX.

 <?php

            //Generate the loop here

            //Prepare arguments for WP_QUERY
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
            $args   = array(

                'posts_per_page'    => 10,
                'post_type'         => 'job',
                'paged'             => $paged

            );

        $query  = new WP_Query( $args );

        if ($query->have_posts()) {

            while ( $query->have_posts()) {
                $query->the_post();
                ?> <li><?php the_title(); ?></li> <?php
            }
        } else {
            echo "<h2>No Jobs Found</h2>";
        }
        // Pagination begins here
        $big = 999999999; // need an unlikely integer
        $paginateArgs = array(
            'base'  => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
            'format' => '/page/%#%',
            'current'   => $paged,
            'total' => $query->max_num_pages
        );
        echo paginate_links( $paginateArgs );
        wp_reset_postdata();
        ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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