根据高级自定义域中的计数更改转发器类



我需要能够根据计数为中继器项目设置不同的类。换句话说,第一个中继器项目需要class="single-item active",所有其他中继器需要class="single-item not-visible move-right"

这是我到目前为止所拥有的:

<?php $count = 0; ?>
      <?php if (have_rows('features')): while (have_rows('features')) : the_row(); ?>
        <?php if(!$count): ?>
        <li class="cd-single-item cd-active"></li>
        <?php else: ?>
        <li class="cd-single-item cd-not-visible cd-move-right"></li> 
      <?php $count++; endif; endwhile; endif; ?>

只需在第一次检查后将布尔值设置为 false:

$repeater = get_field('my_repeater_field');
$first = true;
foreach ($repeater as $sub) {
  $class = $first ? 'single-item active' : 'single-item not-visible move-right';
  // do whatever with $sub and $class here...
  $first = false;
}

你的代码几乎是正确的,但是你只会在(!$count)是假的情况下增加$count,而这永远不会发生,因为你只会增加$count如果......等等。

只需将您的$count++放在第一个endif之后即可。我这样重写它:

<?php
    $count = 0;
    if ( have_rows('features') ):
        while ( have_rows('features') ) : the_row();
            if ( ! $count ): ?>
                <li class="cd-single-item cd-active"></li>
            <?php else: ?>
                <li class="cd-single-item cd-not-visible cd-move-right"></li>
            <?php
            endif;
            $count++;
        endwhile;
    endif;
?>

希望这有帮助!

最新更新