首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress ACF Repeater while循环

Wordpress ACF Repeater while循环
EN

Stack Overflow用户
提问于 2014-05-28 23:26:05
回答 1查看 1.1K关注 0票数 0

您好,我想从div的以下结构,但我不能从while循环中排除div。我的目标是:

代码语言:javascript
复制
<div class="grid-12">
  <img ...>
</div>
<div class="girid-12 parent clear-each-2">
  <div class="grid-6>
     <img ...>
  </div>
  <div class="grid-6>
     <img ...>
  </div>
</div><!-- close clear-each-2 -->

如何从while循环中排除div clear-each-2?

我的代码:

代码语言:javascript
复制
<?php if( have_rows('the_gallery') ):
    $i = 1;
  while( have_rows('the_gallery') ): the_row();
        // vars
        $imageBig = get_sub_field('image_big');
        $imageMedium = get_sub_field('image_medium');
        $imageSmall = get_sub_field('image_small');

        $titleB  = $imageBig['title'];
        $altB     = $imageBig['title'];
        $captionB = $imageBig['caption'];

        $titleM = $imageMedium['title'];
        $altM = $imageMedium['title'];
        $captionM = $imageMedium['caption'];

        $titleS = $imageSmall['title'];
        $altS   = $imageSmall['title'];
        $captionS = $imageSmall['caption'];
        ?>
        <?php if( $imageBig ): ?>
            <div class="grid-12">
                <img src="<?php echo $imageBig['url']; ?>" alt="<?php echo $altB['alt'] ?>" class="max-img" />
                <div class="imgDescriptionSmall float-right"><strong><?php echo $titleB; ?></strong>&nbsp; <?php echo $captionB; ?></div>
            </div>
        <?php endif; ?>
        <?php if( $imageMedium ): ?>
            <div class="gird-12 parent clear-each-2">
                    <div class="grid-6 grid-mobile-12">
                           <img src="<?php echo $imageMedium['url']; ?>" alt="<?php echo $altM['alt'] ?>" class="max-img" />
                           <div class="imgDescriptionSmall float-right"><strong><?php echo $titleM; ?></strong>&nbsp; <?php echo $captionM; ?></div>
                    </div>
            </div>
        <?php endif; ?>
        <?php if ($imageSmall):?>
            <div class="grid-12 parent clear-each-3">
                    <div class="grid-4 grid-tablet-4 grid-mobile-12">
                        <img src="<?php echo $imageSmall['url']; ?>" alt="<?php echo $altS['alt'] ?>" class="max-img" />
                        <div class="imgDescriptionSmall float-right"><strong><?php echo $titleS; ?></strong>&nbsp; <?php echo $captionS; ?></div>
                    </div>
            </div>
            <?php  endif;  $i++; ?>
    <?php endwhile; ?>

最好的,卡罗尔

EN

回答 1

Stack Overflow用户

发布于 2014-05-29 22:33:18

根据您提供的注释和信息,我建议您在原始循环中创建一个数组,然后遍历该数组以创建所需的输出。

代码语言:javascript
复制
<?php if( have_rows('the_gallery') ):
    $count = 0;
    while( have_rows('the_gallery') ): the_row();

        // vars
        $imageBig = get_sub_field('image_big');
        $imageMedium = get_sub_field('image_medium');
        $imageSmall = get_sub_field('image_small');

        $images['large'][$count]['url']       = $imageBig['url'];
        $images['large'][$count]['title']     = $imageBig['title'];
        $images['large'][$count]['alt']       = $imageBig['title'];
        $images['large'][$count]['caption']   = $imageBig['caption'];

        $images['medium'][$count]['url']       = $imageMedium['url'];
        $images['medium'][$count]['title']     = $imageMedium['title'];
        $images['medium'][$count]['alt']       = $imageMedium['title'];
        $images['medium'][$count]['caption']   = $imageMedium['caption'];

        $images['small'][$count]['url']       = $imageSmall['url'];
        $images['small'][$count]['title']     = $imageSmall['title'];
        $images['small'][$count]['alt']       = $imageSmall['title'];
        $images['small'][$count]['caption']   = $imageSmall['caption'];

        $count++;

    endwhile;
endif;?>
<?php if($images['large']):?>
    <div class="grid-12">
    <?php foreach($images['large'] as $img):?>
        <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt'] ?>" class="max-img" />
        <div class="imgDescriptionSmall float-right"><strong><?php echo $img['title']; ?></strong>&nbsp; <?php echo $img['caption']; ?></div>
    <?php endforeach;?>
    </div>
<?php endif;?>
<?php if($images['medium']):?>
    <div class="grid-12 parent clear-each-2">
    <?php foreach($images['medium'] as $img):?>
        <div class="grid-6">
            <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt'] ?>" class="max-img" />
            <div class="imgDescriptionSmall float-right"><strong><?php echo $img['title']; ?></strong>&nbsp; <?php echo $img['caption']; ?></div>
        </div>
    <?php endforeach;?>
    </div>
<?php endif;?>
<?php if($images['small']):?>
    <div class="grid-12 parent clear-each-3">
    <?php foreach($images['small'] as $img):?>
        <div class="grid-4">
            <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt'] ?>" class="max-img" />
            <div class="imgDescriptionSmall float-right"><strong><?php echo $img['title']; ?></strong>&nbsp; <?php echo $img['caption']; ?></div>
        </div>
    <?php endforeach;?>
    </div>
<?php endif;?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23915805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档