Creating a custom category.php page with pagination in WordPress is very useful when we want to put additional content on our category pages.
The code below is an example of how you can create a custom category.php page with pagination in WordPress.
<?php get_header(); ?>
<?php
$article = get_term(get_queried_object()->term_id, 'category');
$category_name = $article->slug;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query(
array(
'category_name' => $category_name,
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '10',
'paged' => $paged,
'post_type' => 'post',
'post_status' => 'publish',
)
); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while( $query->have_posts() ): $query->the_post(); ?>
<div class="category-post">
<a href="<?php the_permalink(); ?>" target="_blank">
<h1 class="category-post-title"><?php the_title(); ?></h1>
<div class="category-post-image">
<?php the_post_thumbnail(); ?>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="pagination original-content">
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'current' => $paged,
'total' => $query->max_num_pages,
'mid_size' => 2,
'prev_text' => __('« Prev Page'),
'next_text' => __('Next Page »')
) );
?>
</div>
<?php wp_reset_query(); ?>
</div>
<?php get_footer(); ?>
There are a few comments I want to share with you so that you understand why the code above works.
First, when I was trying to create a custom category.php page with pagination, I was struggling to get the right wp_query arguments, i.e. the category name (category_name).
Finally, I was able to find that we can get the slug of the category page in the following way:
$article = get_term(get_queried_object()->term_id, 'category');
$category_name = $article->slug;
This allows us to put category_name as an argument into the query.
Next, when doing pagination, we need to set up the query to handle the pagination before thinking about displaying it.
So, we need to create a variable named “paged” and then use it in the arguments in our query.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query(
array(
'category_name' => $category_name,
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '10',
'paged' => $paged,
'post_type' => 'post',
'post_status' => 'publish',
)
);
Finally, to add the pagination links at the bottom of the post, we can use the paginate_links() WordPress function and pass the arguments which make sense for what we are trying to accomplish.
<div class="pagination">
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'current' => $paged,
'total' => $query->max_num_pages,
'mid_size' => 2,
'prev_text' => __('« Prev Page'),
'next_text' => __('Next Page »')
) );
?>
</div>
Depending on what you want to show in the body of your category.php page, you can easily make edits to the main loop. In the example above, the loop is displaying the title of each post, the post thumbnail, and wrapping a post link around these two elements.
You can add the full content by using the_content() WordPress function, or just the excerpt with the_excerpt() WordPress function.
Create Your Custom category.php Page with Pagination in WordPress
Creating custom template pages in WordPress is sometimes tricky, and with so many different resources and web pages out there, we hope that this article has been helpful for you.
By using the code above, hopefully you can create a custom category.php page with pagination in WordPress that suits your needs.