Created
February 2, 2021 15:12
-
-
Save obiPlabon/4886408c8cad59aec928b6d5ff73a3c5 to your computer and use it in GitHub Desktop.
Solution for WP pagination issue on static home page which occurred due to global $paged variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $paged; | |
// Store the global {$paged} value in a temporary {$_temp_paged} variable | |
$_temp_paged = $paged; | |
// Figure out the current page number | |
$_paged = get_query_var( 'paged' ); | |
$_paged = ( ! empty( $paged ) ? $paged : get_query_var( 'page', 1 ) ); | |
// Now store the {$_paged} value in the global {$paged} variable | |
$paged = $_paged; | |
$args = array( | |
'post_type' => 'post', //any post type | |
'posts_per_page' => 3, | |
'post_status' => 'publish', | |
'order' => 'DESC', | |
'paged' => $paged | |
); | |
$query = new \WP_Query( $args ); | |
while ( $query->have_posts() ) : | |
$query->the_post(); | |
the_title( '<h2>', '</h2>' ); | |
endwhile; | |
wp_reset_postdata(); | |
?> | |
<a href="<?php next_posts( $query->max_num_pages ); ?>">Load more</a> | |
<?php | |
// Finally restore the global {$paged} variable using {$_temp_paged} | |
// make sure to restore this after the load more button | |
$paged = $_temp_paged; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment