dpgua04022 2018-12-14 20:41
浏览 35
已采纳

计数器保持在分页的帖子中重置

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 );
  • 写回答

1条回答 默认 最新

  • dongqiongzheng0615 2018-12-15 05:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历