Hasral Ngt Hasral Ngt - 20 days ago 9
PHP Question

Show Count Number

How to show count number in popular post widget?

<div class="terpopuler__num">1</div>


see full code:

<div id="terpopuler" class="terpopuler__row">
<ul class="terpopuler__wrap">
<?php $popular_days_ago = '$popular_days days ago'; $recent = new WP_Query(array( 'posts_per_page' => $number, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
<li class="terpopuler__item">
<a href="<?php the_permalink(); ?>" rel="bookmark">
<div class="terpopuler__num">1</div><!-- count number -->
<div class="terpopuler__title">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
</div>
<?php $post_views = get_post_meta($post->ID, 'post_views_count', true); if ( $post_views >= 1) { ?>
<span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div><!--widget-terpopuler-->


and see my Screanshoot

Answer

Try this, I've added $i top of the while loop, then set $i as auto increment variable ($i++).

<div id="terpopuler" class="terpopuler__row">
    <ul class="terpopuler__wrap">
        <?php $popular_days_ago = '$popular_days days ago'; $recent = new WP_Query(array( 'posts_per_page' => $number, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); 
        $i = 1;
        while($recent->have_posts()) : $recent->the_post(); ?>
        <li class="terpopuler__item">
            <a href="<?php the_permalink(); ?>" rel="bookmark">
                <div class="terpopuler__num"><?php echo $i++; ?></div><!-- count number -->
                <div class="terpopuler__title">
                    <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                </div>
                <?php $post_views = get_post_meta($post->ID, 'post_views_count', true); if ( $post_views >= 1) { ?>
                    <span class="terpopuler__info"><?php hwd_post_views(); ?> kali dibaca</span>
                <?php } ?>
            </a>
        </li>
        <?php endwhile; ?>
    </ul>
</div><!--widget-terpopuler-->