2

I'm trying to add pagination to my archive template. If I use the same code as on my main blog template it doesn't filter results based on the tag in the URL, instead it simply shows all posts. Probably obvious to most but clearly I'm missing something.

I've managed to get the archive page showing just the relevant posts using the following code (but this contains no pagination):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

I've also got my main blog working with pagination using the following code:

<?php 
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $custom_args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'paged' => $paged
    );
    $custom_query = new WP_Query( $custom_args ); 
?>
<?php if( $custom_query->have_posts() ) : while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
    <div class="col span_1_of_1 border_bottom">
        <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="blog_meta_information date">
            Author: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><?php the_author(); ?></a>
            &nbsp;&nbsp;|&nbsp;&nbsp;
            Date: <?php the_time('jS F Y') ?>
            <?php if(has_tag()) { ?>
                <br>
                Tags: <?php the_tags( '',', ','' ); ?>
            <?php } else {} ?>
        </p>
        <p><?php the_field('introduction'); ?></p>
        <a href="<?php the_permalink(); ?>">Read more</a>
    </div>
<?php endwhile; endif; wp_reset_postdata(); ?>
<!-- PAGINATION -->
<?php
    if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
    }
?>

The above also uses the following code from my functions.php file:

// PAGINATION
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    if (empty($pagerange)) {
        $pagerange = 2;
    }
    /**
     * This first part of our function is a fallback
     * for custom pagination inside a regular loop that
     * uses the global $paged and global $wp_query variables.
     * 
     * It's good because we can now override default pagination
     * in our theme, and use this function in default queries
     * and custom queries.
     */
    global $paged;
    if (empty($paged)) {
        $paged = 1;
    }
    if ($numpages == '') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
                $numpages = 1;
        }
    }
    /** 
     * We construct the pagination arguments to enter into our paginate_links
     * function. 
     */
    $pagination_args = array(
        'base'            => get_pagenum_link(1) . '%_%',
        'format'          => 'page/%#%',
        'total'           => $numpages,
        'current'         => $paged,
        'show_all'        => False,
        'end_size'        => 1,
        'mid_size'        => $pagerange,
        'prev_next'       => True,
        'prev_text'       => __('&laquo;'),
        'next_text'       => __('&raquo;'),
        'type'            => 'plain',
        'add_args'        => false,
        'add_fragment'    => ''
    );
    $paginate_links = paginate_links($pagination_args);
    if ($paginate_links) {
        echo "<div class='custom-pagination'>";
            echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
            echo $paginate_links;
        echo "</div>";
    }
}

Anyone know how I can add make this pagination also work with my archive template?

Thanks in advance,

Tom

2 Answers 2

3

Since 4.1.0, WordPress introduce the_posts_pagination to handle number paginate link. I use it all the time, and it just works. With any custom post type. You'll want to use that function after the while loop.

See: https://developer.wordpress.org/reference/functions/the_posts_pagination/

2
  • Thanks for your suggestion but we're still using version 3 on this site. Do you know how I might add it the old fashioned way? Commented Sep 20, 2018 at 8:10
  • Excellent and convenient function. However, you suggest using it after the while loop, but I tried that and it repeated the code above each result. I placed <?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?> immediately above <?php if (have_posts()) : while(have_posts()) : the_post();?> and it worked correctly. (And like you said, "it just works").
    – user38365
    Commented Aug 18, 2020 at 17:08
0

Just a quick update on this one: I chose to use the WP-PageNavi plugin (https://en-gb.wordpress.org/plugins/wp-pagenavi/) on the archives page and leave the main blog using the custom pagination which does the job perfectly.

1
  • This supposed to be part of your question Commented Sep 20, 2018 at 14:14

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.