Dumitru  Barbuta
Dumitru Barbuta

Reputation: 43

Wordpress pagination: get current page ('paged' query_var) from single page URL/template

I have an issue trying to get the current page from WP pagination. Here is my code:

 global $wp_query;

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

   var_dump(get_query_var( 'paged' )); // HERE RETURN 0 EVERYTIME

            $args = [
                'post_type' => 'post',
                'posts_per_page' => 30,
                'paged' => $paged
            ];

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

                    // code ....
            <?php endwhile; ?>

I use this code on a single-page template (and so from a single-page URL). Doing get_query_var( 'paged' ); returns 0.

How to get the current page from a single-page template?

Upvotes: 1

Views: 1393

Answers (1)

Mtxz
Mtxz

Reputation: 3869

As per documentation:

For getting the current pagination number on a static front page (Page template) you have to use the 'page' query variable:

<?php $page = (int)get_query_var( 'page', 1 );  ?>
<h1>Currently Browsing Page <?php echo $page; ?> On a static front page</h1>

Upvotes: 2

Related Questions