• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / Creating Custom category.php With Pagination

Creating Custom category.php With Pagination

November 29, 2021 Leave a Comment

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'       => __('&laquo; Prev Page'),
            'next_text'       => __('Next Page &raquo;')
        ) );
        ?>
    </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'       => __('&laquo; Prev Page'),
            'next_text'       => __('Next Page &raquo;')
        ) );
        ?>
    </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.

Other Articles You'll Also Like:

  • 1.  Get Current Location Latitude Longitude in php
  • 2.  php max – Find Maximum Value of Array in php
  • 3.  php range() – Create Array of Elements Within Given Range
  • 4.  php atan2 – Find Arctangent of the Quotient of Two Numbers
  • 5.  php array_map() – Create New Array from Callback Function
  • 6.  Check if Variable Exists in php with isset() Function
  • 7.  php acos – Find Arccosine and Inverse Cosine of Number
  • 8.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 9.  How to Add Items to Array in php
  • 10.  Using strtoupper() in php to Convert String to Uppercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy