如何回显增量计数器的总数?

时间:2018-12-28 12:06:04

标签: php html wordpress

$ counter变量可以很好地对循环进行计数,但是我需要获取每个循环的元素总数。我将如何去做?

<div id="<?php echo $term->slug; ?>" class="lity-hide resource-pop-up">
<?php 
  if($the_posts->have_posts()): 
    $counter = 1;
    while($the_posts->have_posts()):
      $the_posts->the_post();

    //vars
    $section_one     = apply_filters('the_content', get_field('section_one'));
    $section_two     = apply_filters('the_content', get_field('section_two'));
    $learn_more_link = get_field('learn_more_link');
?>
  <section class="pop-up">
    <div class="title">
      <div class="brand">
        <img src="https://via.placeholder.com/125x125" alt="Brand">
        <?php the_title('<h3>','</h3>'); ?>
      </div>
      <aside>
        <h4><?php echo $counter; ?>/<?php echo $counter->length; ?></h4>
      </aside>

    </div>

    <div class="row pop-up-content">
      <aside class="col-sm-12 col-md-6">
        <?php echo $section_one; ?>
      </aside>

      <aside class="col-sm-12 col-md-6">
        <?php echo $section_one; ?>
      </aside>
    </div>
    <div class="learn-more"><a href="<?php echo $learn_more_link ?>" target="_blank">Learn More</a></div>
  </section>
  <?php
    $counter++;
      endwhile; 
      wp_reset_postdata();
    endif; 
  ?>
</div>

我应该期望(元素数)/(元素总数)或2/10,基本上就像说10中的2。

1 个答案:

答案 0 :(得分:1)

您需要的帖子数

echo $the_posts->post_count 

是所有帖子的总数,而不是

echo $counter->length

$counter只是一个数字,无论如何都没有length属性。