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?

3 Answers 3

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();
4
  • Use <?php the_permalink(); ?> in place of <?php echo $file; ?>
    – Shiv Singh
    Sep 19, 2017 at 11:00
  • 3
    There's a small error in this solution. The code 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) should not have a final parenthesis. In order for the pagination to run correctly remove the closing parenthesis from after the number 1.
    – Pixelsmith
    Apr 18, 2019 at 17:04
  • 1
    query for pagination doesn't work, when i click the second page t returns the first page posts
    – Nadia
    Oct 24, 2020 at 8:57
  • @Jay no it didnt
    – Nadia
    Dec 5, 2020 at 7:25
20

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

5
  • 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 1, 2019 at 2:44
  • 2
    @KhomNazid You are following this permalink structure screencast.com/t/IeYH7bxY3 Right? Aug 1, 2019 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): stackoverflow.com/questions/57301662/…
    – Khom Nazid
    Aug 1, 2019 at 12:18
  • 1
    This is perfect!! @PurvikDhorajiya
    – mjcoder
    May 14, 2021 at 9: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 2, 2021 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>



                   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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