I have created a custom template page for every one of my categories. I am using a foreach loop in order to fetch the posts of this specific category in my page.

if (is_page()) {
    $cat=get_cat_ID($post->post_title); //use page title to get a category ID
    $posts = get_posts ("cat=$cat");
    if ($posts) {
        foreach ($posts as $post):

How can I implement pagination on posts with this, or other code, on custom pages?

  • 1
    Please do a site search on paginating custom queries. This topic has been handled numerous times Oct 15, 2014 at 8:53

3 Answers 3


It's far easier to use built-in loop functions such as pagination when you use a category template based on the template hierarchy.

You mentioned using the category ID to load posts into your custom template page, but if you instead name the template file based on the category ID, you can simply use the default WP loop, and include <?php echo paginate_links(); ?> to display the pagination you seek.

Proper Category Template Naming w/ ID:


Alternatively, if you prefer to press forward using your existing custom category template naming, you can still utilize paginate_links() with $args that correspond with your query.

Read More about paginate_links().


In Custom category page put this code , to work correctly to assign post_type' => 'post

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 
    'prev_text' >' Previous',
    'post_type' => 'post', 
    'posts_per_page' => 2, 
    'paged' => $paged 
$wp_query = new WP_Query($args);

while ( have_posts() ) : the_post(); 
  • Do not break the main query object Dec 28, 2015 at 7:07

try this code just change category name

            $args = array(
            'post_type'=> 'post',
            'category_name'  => 'blog',
              'orderby' => 'post_date',
            'paged' => get_query_var('paged')
            query_posts( $args ); 
            while (have_posts()) : the_post(); ?>

         <?php  the_title(); ?>

       <?php endwhile; ?>

<?php // Wordpress Pagination
                $big = 999999999; // need an unlikely integer
                $links = paginate_links( array(
                    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format' => '?paged=%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total' => $wp_query->max_num_pages,
                    'prev_text'    => '<',
                    'next_text'    => '>',
                    'type' => 'array'
                ) );
                if(!empty($links)){ ?>
                <ul class="pagination">

                        foreach($links as $link){
                            <li><?php echo $link; ?></li>
                        wp_reset_query(); ?>
                    <?php } ?>
  • Sorry, but this is totally incorrect. Also, never ever use query_posts. Unfortunately I can only downvote this once. Apr 4, 2016 at 12:58
  • Also, please correct your formatting, your code is messy and hard to read Apr 4, 2016 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.