Back to Top

How to display paginated posts from one category?

Pagination when getting post from category in Wordpress

One of my all-time favorite topics is WordPress. WordPress is lightweight blogging platform and here is the new article in WordPress about to solve pagination issue with category page in WordPress.

To display paginated posts from one category with WordPress is quite easy if we are using have_post with the while loop. It does not create any issue with have_post but when you go with foreach,get_posts, for loop, wp_query or custom query, your pagination stops working or create some issue. Sometimes you use the multi query or nested loop and its also create issue with pagination.

So, Let’s see how to use pagination when you are working to get posts from the category in WordPress or to get the post in a category page.

First of all declare wp_query global variable.Next create argument $arg_pagi with required current ,total and base parameter and pass it to your pagination function.

Let’s understand by code:

global $wp_query;
$result = new WP_Query('cat=3');//replace with your desired category id
$args_pagi = array(
				'base' => add_query_arg( 'paged', '%#%' ),
				'total' => $result->max_num_pages,
				'current' => $page
			);
			
$args =array('post_type' => 'post','post_status' => 'publish','cat' => 3);
$myposts = new WP_Query( $args );

foreach( $myposts->posts as $data ) : setup_postdata($data); 

// your desired code
endforeach;

//call pagination function
echo paginate_links( $args_pagi);

Also Read:

Fixed Pagination in WordPress
To use Pagination with Taxonomy in WordPress

It’s Wrap. I hope that you find this technique useful to someone.

Don’t hesitate to use this code in your WordPress projects and post your comments if you need help. As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

  1. Hello, i tried your code, but the output is mixed category posts, and counted wrong amount of posts.

    But, its the only code i got that displays a pagination. 😀

    So i have to play a lil more with the code.

    Thanks.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Function handling Functions in PHP

Posted on 7 years ago

Bhumi

How to make a call using Twilio?

Posted on 3 years ago

Ankit Shah

How to handle Exception in PHP5

Posted on 4 years ago

Bhumi

Constructor and Destructor in PHP

Posted on 7 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi