I have created a post to display a list of books. I have added a counter to show the rank of the book.
It's a paginated post with 10 posts/page. The counter keeps getting back to original number when I go to the next page.
e.g :
Page 1 - 100, 99, ... 91
Page 2 - 100, 99, ... 91
...
Page 10 - 100, 99, ... 91
I want the counter to run properly showing 100 - 1.
Here's the code I am using:
<?php
$postidsstring = get_field('post_ids_to_be_included');
$postidsarray = explode(", ",$postidsstring);
$countposts = count($postidsarray);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'books',
'post__in' => $postidsarray,
'orderby' => 'post__in',
'numberposts' => -1,
'paged' => $paged,
'posts_per_page' => 10
);
$myposts = new WP_Query( $args );
$total_pages = $myposts->max_num_pages;
if ( $myposts->have_posts() ) :
while ( $myposts->have_posts() ) : $myposts->the_post(); ?>
<?php echo $countposts; ?>. <?php the_title(); $countposts--; ?>
//Other details
<br>
<?php endwhile;
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 »'),
));
endif;
endif;
wp_reset_postdata();
?>
How do I correct this problem?
Edit: I am using this code to prevent the redirect to Page 1 in function.php:
add_action( 'template_redirect', function() {
if ( is_singular( 'list' ) ) {
global $wp_query;
$page = ( int ) $wp_query->get( 'page' );
if ( $page > 1 ) {
// convert 'page' to 'paged'
$query->set( 'page', 1 );
$query->set( 'paged', $page );
}
// prevent redirect
remove_action( 'template_redirect', 'redirect_canonical' );
}
}, 0 );