16

I have the code below:

<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<div class="col-xs-12 file">
    <a href="<?php echo $file; ?>" class="file-title" target="_blank">
        <i class="fa fa-angle-right" aria-hidden="true"></i> 
        <?php echo get_the_title(); ?>
    </a>
    <div class="file-description">
        <?php the_content(); ?>
    </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>

I am trying to use paginate_links Wordpress function but no matter where I put it, I can't make it work. Can someone help me with this?

roberrrt-s
  • 7,914
  • 2
  • 46
  • 57
Andrei RRR
  • 3,068
  • 16
  • 44
  • 75

3 Answers3

23

Try the code below:

    $the_query = new WP_Query( array('posts_per_page'=>30,
                                 'post_type'=>'phcl',
                                 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                            ); 
                            ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;

$big = 999999999; // need an unlikely integer
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

wp_reset_postdata();
Yamu
  • 1,652
  • 10
  • 15
21

When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.

Example of a custom query:

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

$args = array(
    'post_type'=>'post', // Your post type name
    'posts_per_page' => 6,
    'paged' => $paged,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();

             // YOUR CODE

    endwhile;

    $total_pages = $loop->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 »'),
        ));
    }    
}
wp_reset_postdata();
?>

Example of paginate_links parameters adapted to the custom query above:

For more reference please visit this link

Community
  • 1
  • 1
Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
  • 1
    Thank you for sharing this. It does show me the pagination. But when I click on the URL for Page 2, it still shows me the same content as Page 1. Any ideas? – Khom Nazid Aug 01 '19 at 02:44
  • 2
    @KhomNazid You are following this permalink structure https://www.screencast.com/t/IeYH7bxY3 Right? – Purvik Dhorajiya Aug 01 '19 at 10:44
  • @PurvikDhorajiya, no. I'm following `%category%/%postname%`, which we do need for our blog section outside the custom posts. For custom posts, we rely on the "Permalink Manager Lite" plugin. Our specific case is detailed here (and now our page 2 gives a 404, actually): https://stackoverflow.com/questions/57301662/wordpress-pagination-issues-with-custom-post-type-and-custom-rewrite-rule – Khom Nazid Aug 01 '19 at 12:18
  • 1
    This is perfect!! @PurvikDhorajiya – mjcoder May 14 '21 at 09:03
  • This works well for me thank you, however, it always takes you to the top of the page like you are navigating between different pages, where as I would like it to stay in the same position and just appear to paginate through the post section. Any ideas? Thanks – Mr Toad Nov 02 '21 at 19:35
1
      Try This one, it worked for  me!
    
    <?php
                 $paged = get_query_var('paged');
                    
                $args = array(
                    'post_type' => 'blogs',
                    'paged' => $paged,
                    'orderby' => 'date',
                    'order' => 'ASC',
                    'posts_per_page' => 1,
                    'post_status' => 'publish',
                );
                $the_query = new WP_Query($args);
                if ($the_query->have_posts()) {
                    while ($the_query->have_posts()) {
                        $the_query->the_post();   
            ?>


             Your code...
        <?php
                        }

                    }
                   
     wp_reset_query();
  ?>

<div class="pagination">
<?php
                echo "<div class='fz-pagination'>" . paginate_links(array(
                    'total' => $the_query->max_num_pages,
                    'prev_text' => __('<div class="preious-page">Prev</div>'),
                    'next_text' => __('<div class="next-page">Next</div>')
                )) . "</div>";
            ?>
</div>



                   
Rita Gupta
  • 31
  • 5