1

I am using the loop as you can see in my code. Only 2 posts must be shown and for the rest I should be able to have pagination.

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( 
        array (
            'posts_per_page' => 2,
            'post_type' => 'post', 
            'category_name' => 'news', 
            'category' => 1,
            'paged' => $paged )
    );      
    // The Loop
    while ( have_posts() ) : the_post();?>
        <div class="news-page-content-wrapper">
            <div class="news-page-content">
                <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
                <figure><?php the_post_thumbnail(); ?></figure>
                <p><?php echo get_the_excerpt();?></p>
                <a href="<?php the_permalink(); ?>">Read More&raquo</a>
            </div>
         </div>  
    <?endwhile; 
    // Reset Query
    wp_reset_query();
  ?>
  <?php next_posts_link(); ?>
  <?php previous_posts_link(); ?>

How can I have pagination using the loop with category ID?

Sidney Sousa
  • 3,378
  • 11
  • 48
  • 99

2 Answers2

3
<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$data= new WP_Query(array(
    'post_type'=>'YOUR_POST_TYPE', // your post type name
    'posts_per_page' => 3, // post per page
    'paged' => $paged,
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();
            // Your code
    endwhile;

    $total_pages = $data->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Pagination Like : Prev 1 2 3 Next

Would you please try above code? check my answer for more information.

Community
  • 1
  • 1
Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
1

For navigation you can use a plugin. Click here

and use <?php wp_pagenavi(); ?> instead of

<?php next_posts_link(); ?>
  <?php previous_posts_link(); ?>
Sukhdeep Kaur
  • 203
  • 2
  • 11