Can paginate_links() be customized for a specific category or tag?


I am creating category and tag page templates for my client so they can post news articles to the news category page and specific news pages separated by tag with a pagination nav at the bottom of each page. What I noticed, however, is the pagination nav takes account of all of the posts belonging to any category or having any tag.

I am aware that paginate_links() does not pass an argument in any way similar to the category_name argument for WP_Query(). Keeping this in mind, I spent Googling for a solution to excluding all of the irrelevant posts from the scope of paginate_links() with no luck.

Can you write paginate_links() so that the function targets a specific category or tag? If so, please help me work out how.

The below is the code including the query and pagination function:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'post',
            'category_name' => 'NEWS',
            'tag_slug__and' => $tags,
            'posts_per_page' => 2,
            'paged' => $paged
    $the_query = new WP_Query($args); ?>

<?php if ( $the_query->have_posts() ) :  ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
            <div class="inner">
                <?php the_content(); ?>
    <?php endwhile; ?>

    <div class="pagination">
            echo paginate_links( array(
                'format'  => 'page/%#%',
                'current' => $paged,
                'total'   => $the_query->max_num_pages,
                'mid_size'        => 2,
                'prev_text'       => __('&laquo; Previous Page’),
                'next_text'       => __(‘Next Page &raquo;')
            ) );

<?php endif; ?>

Thank you for reading this.


Fizzler 8 months 2020-11-23T08:10:19-05:00 0 Answers 9 views 0

Leave an answer