【问题标题】:How to count the total number of rows in a ACF repeater output如何计算 ACF 转发器输出中的总行数
【发布时间】:2017-10-02 22:41:00
【问题描述】:

问题:如何简单地计算 ACF 转发器字段输出中的行数?

目标:当只有一行而不是多行时,使用 css 类使输出看起来不同。

我的代码:

if( have_rows('testimonials')) {
    $counter = 0;
    $numtestimonials = '';

    //loop thru the rows
    while ( have_rows('testimonials') ){
        the_row();
        $counter++;
            if ($counter < 2) {                         
                $numtestimonials = 'onlyone';               
            }
        echo '<div class="testimonial ' . $numtestimonials . '">';
           // bunch of output here
        echo '</div>';                          
    }
}

显然,我在这里执行的方式不起作用,因为第一次通过该行的计数为

谢谢!

【问题讨论】:

    标签: php mysql wordpress repeater advanced-custom-fields


    【解决方案1】:

    好的,我终于找到了答案。

    计算 ACF 转发器中总行数的方法是:

    $numrows = count( get_sub_field( 'field_name' ) );
    

    【讨论】:

    • 谢谢。如果转发器不是子字段(典型情况),那么它只是count( get_field( 'field_name' ) );
    • Ben(上图)也是正确的。他的评论帮助了我。
    • 这仍然对我不起作用。我在灵活的内容布局中有一个中继器。 have_rows/the_row while 循环可以很好地打印转发器的内容,但在转发器名称上调用的 get_sub_fieldget_field 都不会返回任何内容。如果我error_logget_sub_fieldget_field 的输出,我可以看到两者都没有返回,这就是我无法获得计数的原因。我的临时解决方法是在主要循环之前重复 have_rows/the_row while 循环,只是为了计算中继器中的所有行数:/ 我讨厌这个解决方案。
    • 在内容循环之前,你有没有解决这个荒谬的不必要循环?
    • 我有 5 个子字段,但我得到了 24 个,所以这似乎不再有效。
    【解决方案2】:

    你可以像这样得到行数:

    $count = get_post_meta(get_the_ID(), 'testimonials', true);
    

    显然这使用get_the_ID() 来检索当前的帖子 ID - 您可能需要修改它。

    ACF 将转发器计数值与转发器字段名称存储为 postmeta 表中的 meta_key。

    ACF 使用计数来检索正确的转发器子字段值,这些值存储为针对 meta_keys 的值,格式为 $repeaterFieldname . '_' . $index . '_' . $subfieldName

    希望这会有所帮助...

    【讨论】:

    • 感谢大卫,这确实有助于了解 ACF 如何处理它。我想获得总行数,而不仅仅是当前行 ID。我不知道如何做到这一点。
    • 嗨罗伯特,我认为它完全符合您的需求。例如,如果您为“testimonials”转发器添加了 4 个转发器条目,get_post_meta(get_the_ID(), 'testimonials', true) 将返回“4”,而不是行 ID。
    • 这条评论被低估了 - 非常适合获取我的转发器的行数。
    【解决方案3】:

    对我有用,count 必须放在 if(have_rows('repeater_field') 之前:

    中继器为空时避免警告错误的三元运算符

    如果将计数放在“if(have_rows('repeater_field')) :”之后,计数返回 FALSE

    $repeater_field = get_sub_field('repeater_field');
    // OR if repeater isn't a sub_field
    // $repeater_field = get_field('repeater_field');
    
    // ternary operator to avoid warning errors if no result
    $count =  $repeater_field ? count($repeater_field) : FALSE;
    
    if(have_rows('repeater_field')) : // OR if($count) :
    
        echo 'Number of posts:' . $count . '<br>';
    
        while(have_rows('repeater_field')) : the_row();
            echo get_sub_field('field_name') . '<br>';
        endwhile;
    endif;
    

    【讨论】:

    • 请在您的回答中添加一些解释。
    • 够明确吗?
    【解决方案4】:

    你可能想试试这个..

    <?php 
    $row = get_field('repeater', $post->ID);
    if($row < 1) {
     $rows = 0;
    } else {
     $rows = count($row);
    } ?>
    <p>Number of Row is (<?php echo $rows ; ?>)</p>
    

    【讨论】:

      【解决方案5】:

      您似乎想要重置$numtestimonials 中的值。

      因此,有效地,带有修复的代码将是:

      $output = "";
      $numtestimonials = "";
      
      while ( have_rows('testimonials') ){
          the_row();
          $counter++;
          $output .= "<span>" .$some_data. "</span>"; // bunch of output;
      }
      
      if($counter < 2){
          $numtestimonials = "onlyone";
      }
      $output = "<div class='testimonail ".$numtestimonials." '>"
                    .$output
               ."</div>";
      echo $output;
      

      【讨论】:

      • 您好,这个解决方案的问题是第一行得到“onlyone”类,即使总行数超过一个。
      • 嗨@罗伯特。请查看编辑后的代码。它应该照顾您描述的情况 - 以防万一您仍然想按照这些方式做一些事情。虽然很高兴看到您自己找到了提前计算行数的方法!
      【解决方案6】:

      高级自定义字段有一个内置函数来计算版本5.3.4中添加的行数。

      官方文档:ACF | get_row_index()

      您可以在循环内使用get_row_index();

      <?php if( have_rows('slides') ): ?>
          <?php while( have_rows('slides') ): the_row(); ?>
              <div class="accordion" id="accordion-<?php echo get_row_index(); ?>">
                  <h3><?php the_sub_field('title'); ?></h3>
                  <?php the_sub_field('text'); ?>
              </div>
          <?php endwhile; ?>
      <?php endif; ?>
      

      此函数返回的索引从 1 开始。这意味着具有 3 行数据的 Repeater 字段将生成索引 1、2 和 3。

      【讨论】:

        【解决方案7】:

        你可以试试这段代码,它工作正常:

        <?php if( have_rows('repeater_name') ):
        $my_fields = get_field_object('repeater_name');
        $count = (count($my_fields));
        echo $count;
        endif;?>
        

        【讨论】:

          【解决方案8】:

          ACF Repeater Field 使用数组来存储行 获取字段行数的最简单方法是

          $count = sizeof(get_sub_field('field_name'));
          

          【讨论】:

          • 嗨,欢迎来到 SO !虽然您的答案可能是正确的,但已经有一个公认的答案与您的答案几乎相同。尽量不要发布重复的答案。如果接受的答案有误或您对其进行了改进,请在答案中提供更多详细信息。
          • 当我使用 count();另一方面,php 显示警告 sizeof() 的工作没有警告。
          • 请在您的答案中包含此信息,以便其他人可以更好地看到它。还包括您收到的警告。这将大大改善您的答案!
          猜你喜欢
          • 2023-02-14
          • 2019-05-24
          • 1970-01-01
          • 2019-11-19
          • 2022-11-02
          • 2013-04-01
          • 1970-01-01
          • 2021-01-04
          • 1970-01-01
          相关资源
          最近更新 更多