ACF中继器显示行号

时间:2016-03-16 11:09:59

标签: count repeater rows advanced-custom-fields

希望显示每个中继器行的个别编号。例如,第一行将显示" 1"第二行将显示" 2"。

我是从艾略特那里找到的,

<?php echo count( get_field('repeater_field') );?>

计算总共有多少行。但我需要旁边的个人号码。

由于

2 个答案:

答案 0 :(得分:2)

我建议在这里看看艾略特的答案:

http://support.advancedcustomfields.com/forums/topic/getting-instance-and-sort-of-id-of-repeater-field/

您可能需要设置一个计数器变量($ i),然后在循环内向$ i添加1。

<?php if( have_rows('repeater_field') ): $i = 0; ?>
    <div class="repeater_loop">
    <?php while( have_rows('repeater_field') ): the_row(); $i++; ?>
        <p>This is row number <?php echo $i; ?>.</p>
        <!-- call your sub_fields as needed -->
    <?php endwhile; ?>
    </div>
<?php endif; ?>

这将输出一个div,其中包含一个显示行号的段落标记。

答案 1 :(得分:0)

<?php if( have_rows('tabel_produse_profit') ): $i = 0; ?>

<table width="100%" class="tab">
  <tr>
    <td>Nr.</td>
    <td>Imagine</td>
    <td>Nume</td>
    <td>Evaluare</td>
    <td>Verificati pretul</td>
  </tr>

    <?php while( have_rows('tabel_produse_profit') ): the_row(); $i++;
        // vars
        $image = get_sub_field('tabel_imagine_produs');
        $link = get_sub_field('tabel_link_profit');
        $titlu = get_sub_field('tabel_titlu_profit');
        $evaluare = get_sub_field('tabel_evaluare');
        $count = count(get_field('tabel_produse_profit'));
        ?>
        <tr>
            <td class="row1">
            <?php echo $i; ?>
                <?php if( $count ): ?>
                    <div class="rowc"> din <?php echo $count; ?></div>
                <?php endif; ?>
            </td>
           <td class="row2">
                <?php if( $image ): ?>
                    <img class="imag" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
                <?php endif; ?>
           </td>
           <td class="row3">
                <?php if( $titlu ): ?>
                    <h2><?php echo $titlu; ?></h2>
                <?php endif; ?>
           </td>
           <td class="row4">
                <?php if( $evaluare ): ?>
                    <div class="eva"><?php echo $evaluare; ?></div>
                <?php endif; ?>
           </td>
           <td class="row5">
                <?php if( $link ): ?>
                    <a class="ver" href="<?php echo $link; ?>">VERIFICAȚI PREȚUL</a>
                    <div class="mag">pe emag.ro</div>
                <?php endif; ?>
           </td>
            <?php echo $content; ?>
        </tr>
      <?php endwhile; ?>  
    </table>
<?php endif; ?>