ACF 中继器显示行号



希望显示每个转发器行的单个编号。例如,第一行将显示"1",第二行将显示"2"。

我从艾略特那里找到了这个,

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

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

谢谢

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

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

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

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

已经有 get_row_index() 可供使用。

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

最新更新