How to create pagination on archive.php template

Question

I’m creating a custom theme and I’m struggling to get pagination working on my archive.php template.

I’ve tried to adapt the code I’m using on my main blog page, but it’s simply displaying all blog posts rather than just those with a certain tag or category.

Any ideas where I’m going wrong and how to get the archive.php template working as it should be WITH pagination?

Thanks in advance,

Tom

<?php get_header(); ?>

    <!-- PAGE INTRODUCTION -->
    <div class="container">
        <h1 class="page_title"><?php the_archive_title(); ?></h1>
    </div>

    <!-- PAGE CONTENTS -->
    <div class="container">
        <div class="row">
            <?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="item">
                    <h3><?php the_title(); ?></h3>
                    <a href="<?php the_permalink(); ?>">Read more</a>
                </div>
            <?php endwhile; endif; wp_reset_postdata(); ?>
        </div>

        <!-- PAGINATION -->
        <?php
            if (function_exists(custom_pagination)) {
                custom_pagination($custom_query->max_num_pages,"",$paged);
            }
        ?>

    </div>

<?php get_footer(); ?>

in progress 0
Tom Perkins 2 weeks 2022-01-05T09:41:32-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2022-01-07T01:42:57-05:00

    The solution was surprisingly simple as you can see below.

    <?php get_header(); ?>
    
    <!-- PAGE INTRODUCTION -->
    <div class="container">
        <h1 class="page_title"><?php the_archive_title(); ?></h1>
    </div>
    
    <!-- PAGE CONTENTS -->
    <div class="container">
        <div class="row">
    
            <!-- POSTS -->
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <div class="item">
                    <h3 class="subtitle no_margin_bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <a href="<?php the_permalink(); ?>">Read more</a>
                </div>
            <?php endwhile; endif; wp_reset_postdata(); ?>
    
            <!-- PAGINATION -->
            <?php the_posts_pagination(); ?>
    
        </div>
    </div><!-- END OF SECTION -->
    
    <?php get_footer(); ?>

Leave an answer

Browse
Browse