Step by Step Guide for Category Pagination in WordPress
WordPress provides an inbuilt feature used to navigate through posts. Developers of themes can make use of numbered pagination of straightforward links...
WordPress provides an inbuilt feature used to navigate through posts. Developers of themes can make use of numbered pagination of straightforward links to mark or distinguish the next page or the previous page in a sequence.
WordPress can be able to divide one post or multiple posts into various pages so that “paged” navigation can be enabled. The number of posts that will comprise each page on the Reading Screen can be set using:
Wp-admin > Settings > Reading
Except it is overridden by your theme, WordPress will make use of its default option to decide the number of blog posts present on one page. An example where WordPress might decide to override this value is when a custom query is being used.
WordPress makes use of pagination when:
There are too many posts and all cannot fit on one page;
Dividing longer posts manually by using <!--nextpage-->
This is the most popular use of pagination in WordPress sites. Long lists of posts are broken into different pages. Normally, WordPress can only show 10 posts per page and it doesn't matter if you're viewing an archive or a default index page. This, however, can be changed using:
Admin > Settings > Reading
Displaying Pagination Links
WordPress is blessed with different functions for showing links in various pages in your loop. However, some of these functions can only be used in unique contexts. Single post pages and archive pages possess different functions.
Simple Pagination
There are different methods available but one of the simplest is posts_nav_link(). All you have to do is place the function in your template immediately where the loop ends. This will create links to the next page and previous page (if applicable) of posts. This function is suitable for Wordpress Theme Development with simple pagination requirements.
Numerical Pagination
When multiple pages are present, it will be more-ideal to create a list of page numbers so it will be easy to navigate. With the list of page numbers, there won't be any need to repeatedly click the next or previous posts. There are several functions provided for numerical pagination lists.
To ensure a more extensive pagination option, you can make use of the_posts_pagination() for WordPress 4.1 and all other higher versions. This will ensure that links to previous and next pages of posts are provided.
b) For WordPress before the 4.1 Version
Use paginate_links() if you want your pagination to support WordPress versions before 4.1.
Pagination Between Single Posts
The previous functions are to be utilized on archive and index pages. To view a single blog post, you must make use of prev_post_link and next_post_link. You'll have to place the below functions at the end of the loop on your single.php.
1 | previous_post_link();
2 | next_post_link();
Pagination Within a Post
WordPress makes a tag available. This tag can be placed in post content so that pagination for that post can be enabled.
1<!--nextpage-->
If you're making use of the tag in the content, you have to make sure that the wp_link_pages functions is arranged in your single.php template inside your loop.
1 | <?php if ( have_posts() ) : ?>
2 |
3 | <!-- Start of the main loop. -->
4 | <?php while ( have_posts() ) : the_post(); ?>
5 |
6 | <?php the_content(); ?>
7 |
8 | <?php wp_link_pages(); ?>
9 |
10 | <?php endwhile; ?>
11 |
12 | <?php endif; ?>
Category Pagination in WordPress
Category pagination in WordPress can honestly be a bit dicey in its execution. In this post, we'll assume that you're establishing a custom category page (almost like category.php) in your theme file.
As we commence, we must establish the current category of the page. This would differ when considering the category the user clicked on to arrive on this page. To find out the current category, put the following code in your code editor:
We now have the current category saved in $cat_id. We'll now start with the second part which entails establishing unique arguments for your loop and the real loop. It should be noted that putting an offset number into the loop will break it.
The above is a random example of what your code might look like. It definitely will be more complex than the example.
End the loop once you're done with the HTML/PHP.
1 | <?php endwhile; ?>
After ending the loop, the final step should be started. This step will bring everything together and ultimately insert the pagination links to the page. Wrapping this code in a div isn't compulsory, but I decided to do it anyway. You can also change the prev_text and next_text to anything of your preference.
Nathan Beers is a former professional poker player turned developer. He is currently living in Tampa, FL and working as a full-stack web developer. He is really very fascinated by learning new technologies and love to write about them. He intensely believes in a decentralized internet with no government involvement and enjoys trading cryptocurrencies.
4 Replies to “Step by Step Guide for Category Pagination in WordPress”
Hey Nathan Beers
Why do you store the query inside the $temp when you never use it again?
I guess you forgot to assign the query back to it’s default value?
Then why do you do this:
$wp_query = null;
$wp_query = new WP_Query();
Shoudn’t the second line do all the work?
Best regards
It was really insightful.
Thanks for the info.
Wanna have more contents from you.
Cheers
It was really insightful.
Thanks for the info.
Wanna have more contents from you.
Cheers
hey,
i read your post on pagination on WordPress. I have to say, I’m very impressed with your perspectives and opinions. keep sharing your valuable thoughts.
Thanks-
Hey Nathan Beers
Why do you store the query inside the $temp when you never use it again?
I guess you forgot to assign the query back to it’s default value?
Then why do you do this:
$wp_query = null;
$wp_query = new WP_Query();
Shoudn’t the second line do all the work?
Best regards
It was really insightful.
Thanks for the info.
Wanna have more contents from you.
Cheers
It was really insightful.
Thanks for the info.
Wanna have more contents from you.
Cheers
hey,
i read your post on pagination on WordPress. I have to say, I’m very impressed with your perspectives and opinions. keep sharing your valuable thoughts.
Thanks-