Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果WordPress中继器有1个字段、2个字段等_Php_Wordpress_If Statement_Advanced Custom Fields - Fatal编程技术网

Php 如果WordPress中继器有1个字段、2个字段等

Php 如果WordPress中继器有1个字段、2个字段等,php,wordpress,if-statement,advanced-custom-fields,Php,Wordpress,If Statement,Advanced Custom Fields,我有一个中继器,我想根据它输出的字段数进行修改 我拥有的基本修复程序代码: <?php // REAPEATER FIELD if(get_field('whatever')): ?> <?php while(has_sub_field('whatever')): ?> <div class="whatever_block"> <?php the_sub_field('anything'); ?> </div

我有一个中继器,我想根据它输出的字段数进行修改

我拥有的基本修复程序代码:

<?php // REAPEATER FIELD
if(get_field('whatever')): ?>
  <?php while(has_sub_field('whatever')): ?>

    <div class="whatever_block">
      <?php the_sub_field('anything'); ?>
    </div>

  <?php endwhile; ?>
<?php endif; ?>

我希望它看起来怎么样:

<?php // REAPEATER FIELD
if(get_field('whatever')): ?>
  <?php while(has_sub_field('whatever')): ?>

    //if only one
    <div class="col-sm-12">
      <?php the_sub_field('anything'); ?>
    </div>

    //if only two
    <div class="col-sm-6">
      <?php the_sub_field('anything'); ?>
    </div>

    //if etc

    //else
    <div class="whatever_block">
      <?php the_sub_field('anything'); ?>
    </div>

  <?php endwhile; ?>
<?php endif; ?>

//如果只有一个
//要是两个就好了
//如果等
//否则
如何使用if语句实现上述输出?if语句是否正确?

好的,这将使用count()函数对字段进行计数,然后使用开关大小写为div设置一个类,如下所示:

<?php // REAPEATER FIELD
if(get_field('whatever')): ?>
    <?php switch(count(get_field('whatever'))) :    //checks total fields set
        case '1':
            $divClass = 'oneField';
            break;
        case '2':
            $divClass = 'twoField';
            break;
        case '3':
            $divClass = 'threeField';
            break;
        case '4':
            $divClass = 'fourField';
            break;
    endswitch; ?>
    <?php while(has_sub_field('whatever')): ?>

        <div class="<?php echo $divClass; ?>">
          <?php the_sub_field('anything'); ?>
        </div>

    <?php endwhile; ?>
<?php endif; ?>


您可以在中继器上进行
计数
,以检查它有多少子字段。下面是一个使用ACF v4.xx的解决方案(尽管如此,也应使用
get\u字段
):

if(有_行('parent_where')){
$posts_no=get_字段('parent_whatever');
while(有_行('parent_whatever')){
_行();
$class=“col sm-”.12/$posts\u no;
回声';
_sub_字段('whatever');
回声';
结束时;
endif;

你的问题是什么?@Celeo,你发表评论时我正在更新我的问题。我正在尝试实现第二个版本(我希望它看起来如何).所以我想用一个if语句,我会根据有多少项来更改我输出的内容的类别。这更有意义吗?一次活动的字段是否有最大数量?就像在ACF中一样,您是否设置了最大数量?@Joe,是的,我将最大数量设置为4如果您尝试我的答案,我会发现一些小的代码问题我刚刚在编辑中修复了这个问题,所以这段代码现在应该可以工作了。
if( have_rows( 'parent_whatever' ) ) {
    $posts_no = get_field( 'parent_whatever' );
    while( have_rows( 'parent_whatever' ) {
        the_row();
        $class = "col-sm-" . 12/$posts_no;

        echo '<div class="' . $class . '">';
        the_sub_field( 'whatever' );
        echo '</div>';
    endwhile;
endif;