显示计数

时间:2016-11-21 03:39:29

标签: php wordpress count

如何在热门帖子小工具中显示计数?

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

查看完整代码:

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

看到我的Screanshoot
Screanshoot http://image.prntscr.com/image/5d0df6cf07504909b10fafa4d6d40ed6.png

2 个答案:

答案 0 :(得分:1)

试试这个,我在while循环中添加了$i,然后将$ i设置为自动增量变量($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-->

答案 1 :(得分:0)

<div id="terpopuler" class="terpopuler__row">
        <ul class="terpopuler__wrap">
            <?php $i = 0; $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"><?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-->