Add pagination to custom page template

Viewed 173

I added pagination to my custom page template, but it won't show up. If I use this code as custom category template - it works with pagination. What's wrong?

I control number of post per page from WordPress settings. If I set static in loop it also works, but no pagination in any configuration.

Here's my code:

<?php

get_header(); ?>

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

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

<div class="press-cat">
    <?php
    $category = get_the_category();
    $current_category_ID = isset($category->cat_ID) ? $category->cat_ID : 0;
    $args = array(
        'taxonomy' => 'category',
        'hide_empty' => 0,
        'hierarchical' => 1,
        'child_of' => 420,
        'title_li' => 0,
        'current_category' => $current_category_ID
    );
    wp_list_categories($args);
    ?>
</div>


        <?php 
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>0)); ?>
 
<?php if ( $wpb_all_query->have_posts() ) : ?>


<div class="press-items">
<ul>
 
    <!-- the loop -->
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <li>
            <div class="press-date">
                <a href="<?php the_permalink(); ?>">
                    <?php $post_date = get_the_date( 'F j, Y' ); echo $post_date; ?>
                </a>
            </div>
            <div class="press-title">
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>  
            <div>
        </li>
    <?php endwhile; ?>
    <!-- end of the loop -->
 
</ul>
</div>

<?php // Wordpress Pagination
                $big = 999999999; // need an unlikely integer
                $links = paginate_links( array(
                    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format' => '?paged=%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total' => $wp_query->max_num_pages,
                    'prev_text'    => '<',
                    'next_text'    => '>',
                    'type' => 'array'
                ) );
                if(!empty($links)){ ?>
                <ul class="pagination">
                        <?php

                        foreach($links as $link){
                            ?>
                            <li><?php echo $link; ?></li>
                            <?php
                        }
                        wp_reset_query(); ?>
                    </ul>
                    <?php } ?>

    <?php wp_reset_postdata(); ?>
 
<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
        </div><!-- #content -->     
    </div><!-- #primary -->
</div><!-- #main-content -->

<?php
get_footer();
1 Answers

You have to define the "posts_per_page" in your WP_Query statement, as I can see you have defined with Zero (0)

Please set with 10 or 20 or 30 etc and

$wpb_all_query->max_num_pages variable instead of $wp_query->max_num_pages in your pagination code as your query stored in $wpb_all_query variable in your code example

Use this code for pagination

$big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wpb_all_query->max_num_pages
) );

And please add this new argument in your query code

'paged' => get_query_var('paged') ? get_query_var('paged') : 1

So your query should be this

$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>0, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1));
Related