简体   繁体   中英

counter keeps resetting in a paginated post

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.

eg :

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 );

I found a solution though not sure if its the right way of doing this but it works for me. I added another counter which would use the value in the variable countposts, current_page and posts_per_page to show the correct counter. Here is it:

<?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() ) :
    $current_page = max(1, get_query_var('paged'));
    while ( $myposts->have_posts() ) : $myposts->the_post(); 
        $counter = $countposts-(($current_page-1)*$posts_per_page); ?>
        <?php echo $counter; ?>. <?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(); 
?>

I got this solution from an old blog post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM