0

I'm trying to index my posts (in reverse) and I'm using current_count within loop to do this. This is my code:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'html5-blank',
        'posts_per_page' => 5,
        'paged' => $paged,
        'category_name' => get_query_var('category_name')
    );
?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post();

    // current page number - paged is 0 on the home page, we use 1 instead
    $_current_page  = is_paged() ?  get_query_var( 'paged', 1 ) : 1;

    // posts per page
    $_ppp           = get_query_var( 'posts_per_page', get_option( 'posts_per_page' ) );

    // current post index on the current page
    $_current_post  = $loop->current_post;

    // total number of found posts
    $_total_posts   = $loop->found_posts;
?>
<div class="col-xs-12 col-lg-10 col-lg-offset-1 video-short">
    <div class="row">
        <div class="video-short-img col-xs-5 col-lg-3 col-md-3">
            <a href="<?php the_permalink() ?>">
                <div class="featured-img featured-img-small" style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>);">
                    <div class="video-blog-link">
                        <span class="icon-View-Icon"></span>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-xs-7 col-lg-8 col-md-8 col-md-offset-1 video-short-body">
                <div class="video-short-headline">
                    <h3 class="post-num"><?php echo $counter = $_total_posts - ( $_current_page - 1 ) * $_ppp - $_current_post; ?></h3>
                <a href="<?php the_permalink() ?>">
                    <h4 class="post-headline text-uppercase"><?php the_title() ?></h4>
                </a>
                </div>
            <div class="post-body-short">
                <p><?php the_excerpt(); ?></p>
                <div class="video-short-tags-cats">
                    <div class="subject">
                        <p>
                            <?php foreach ((get_the_category()) as $category) {
                                echo $category->cat_name . ' ';
                            } ?>
                        </p>
                    </div>
                    <div class="tags">
                        <p>
                            <?php
                                $posttags = get_the_tags();
                                if ($posttags) {
                                    foreach($posttags as $tag) {
                                        echo $tag->name . ', ';
                                    }
                                }
                            ?>
                        </p>
                    </div>
                </div>
            </div>
        </div><!-- END video-short-body -->
    </div><!-- END row -->
</div><!-- END VIDEO-SHORT -->
<?php endwhile; ?>

<?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1
    $nextUrl = get_next_posts_page_link();
    $prevUrl = get_previous_posts_page_link();
?>
<div class="paginations clearfix">
    <ul class="pagination">
        <li><a href="<?php echo esc_url( $prevUrl ); ?>"><span class="icon-left_arrow"></span></a></li>
        <!-- pagination here -->
        <?php
          if (function_exists(custom_pagination)) {
            custom_pagination($loop->max_num_pages,"",$paged);
          }
        ?>
        <li><a href="<?php echo esc_url( $nextUrl ); ?>"><span class="icon-right_arrow"></span></a></li>
    </ul>
</div>
<?php } ?>

now this works fine, but it breaks with pagination. On first page everything is fine ,but when I change the page it shows first post of second page with -4 index. Here are the images of page one and two: enter image description here enter image description here This post should have number 1.
Can somebody tell what I'm doing wrong and how to fix it ?

  • if you have 12 elements e.g., you want that the first page show 12, 11, 10 and 9 ? – mmm Nov 19 '17 at 16:43
  • @mmm Yeah I show 5 posts per page and want them in order you described. – Karadjordje Nov 19 '17 at 16:45
  • maybe $_current_post contain the global index then just use $counter = $_total_posts - $_current_post; – mmm Nov 19 '17 at 16:48
  • Well now on my second page it shows number 6, basically pagination breaks counter it starts from total post number again, but it should continue from the last number on the first page. If the last number on first page was 3 then first number on second page should be 2. – Karadjordje Nov 19 '17 at 16:57
  • use this code to debug and then edit your question to show screenshots of page 1 and 2 echo "<pre>"; var_export(compact("_current_post", "_current_page", "_ppp", "_total_posts")); echo "</pre>"; – mmm Nov 19 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.