当重复字段包含 6 个以上的值时创建一个新的引导列

我正在尝试使用 ACF 中继器字段和自定义帖子类型构建餐厅菜单,自定义帖子类型的标题告诉类别,例如烧烤,而repeater_fields 显示菜单项问题是我想创建一个新的引导列li 在循环中达到 6 个项目。


<div class="fadeIn col-lg-12 col-sm-12 col-md-12" >

<?php $loop = new WP_Query(array('post_type'=>'menu'));

    if($loop->have_posts()) :

        $i=0;

        ?>

        <div class="row">

    <?php while($loop->have_posts() ): $loop->the_post(); ?> 

<ul style="width:50%" class="menu-list col-md-6 column  <?php echo the_title(); ?>">

        <?php while(the_repeater_field('item')):  ?>

        <li style="max-width:1000px;"><strong class="icon">&#x2022;</strong>&nbsp;<b><?php the_sub_field('heading'); ?></b><span class="price" >1.95</span><br><strong style="font-weight:400;margin-left:22px;">Soybean paste broth</strong></li>

        <?php  endwhile;?>  

    </ul>

 <?php endwhile;  endif; ?>

</div>


</div>

如果 li 中的项目超过 5 项,我想自动创建一个新的 col-md-6 ul


千巷猫影
浏览 109回答 3
3回答

潇潇雨雨

也许这样的事情可以工作?&nbsp; &nbsp; <div class="fadeIn col-lg-12 col-sm-12 col-md-12" ><?php $loop = new WP_Query(array('post_type'=>'menu'));&nbsp; &nbsp; if($loop->have_posts()) :&nbsp; &nbsp; &nbsp; &nbsp; $i=0;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; <?php while($loop->have_posts() ): $loop->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php $i = 0; ?>&nbsp; &nbsp; &nbsp; &nbsp; <ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; <?php while(the_repeater_field('item')):&nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php ++$i; ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php if($i % 6 == 0): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <li style="max-width:1000px;"><strong class="icon">&#x2022;</strong>&nbsp;<b><?php the_sub_field('heading'); ?></b><span class="price" >1.95</span><br><strong style="font-weight:400;margin-left:22px;">Soybean paste broth</strong></li>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; endwhile;?>&nbsp;&nbsp;&nbsp; &nbsp; </ul>&nbsp;<?php endwhile;&nbsp; endif; ?></div></div>编辑:因为我还不能发表评论......@Bas Mulder 发布的批准的答案是行不通的,如果你有超过 12 个条目:<div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <?php while($loop->have_posts() ): $loop->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j = 0; ?>&nbsp; &nbsp; &nbsp; &nbsp; <ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while(the_repeater_field('item')):&nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ($j === 5): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul><ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j = 0; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li style="max-width:1000px;"><strong class="icon">&#x2022;</strong>&nbsp;<b><?php the_sub_field('heading'); ?></b><span class="price" >1.95</span><br><strong style="font-weight:400;margin-left:22px;">Soybean paste broth</strong></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j++; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; endwhile;?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile;&nbsp; endif; ?>&nbsp; &nbsp; </div>第二次,它将在 5 个条目上创建新的 UL。因为一旦创建了新的 UL,j 就会设置为 0 并立即增加到 1,所以一旦您的代码命中 11 元素,就会创建新的 UL。编辑 2:由于问题,我更新了我的答案,所以最初$i++;改为++$i;第二种解决方案是移动 $i++; 下面如果:&nbsp; &nbsp;<?php if($i % 6 == 0): ?>&nbsp; &nbsp;<?php ++$i; ?>

回首忆惘然

我正在添加另一个答案,因为它与原始问题没有直接关系。根据评论,要拥有两个带有 li 元素的 ul,您可以这样做:$my_fields = get_field_object('item');&nbsp;$count = count(($my_fields['value']));&nbsp;$showFirst = ceil($count/2);$showSecond = $count - $showFirst;&nbsp;比你做这样的事情:<?php $show = $showFirst; ?>&nbsp;<ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp;<?php for($i = 0; $i < $show; $i++):&nbsp; ?>&nbsp; &nbsp; <li style="max-width:1000px;">&nbsp; &nbsp; &nbsp; &nbsp; <strong class="icon">&#x2022;</strong>&nbsp;<b><?php the_sub_field('heading'); ?></b><span class="price" >1.95</span><br><strong style="font-weight:400;margin-left:22px;">Soybean paste broth</strong>&nbsp; &nbsp; </li>&nbsp;<?php if($i == ($show - 1)): ?>&nbsp;</ul>&nbsp;<ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; $i = 0;&nbsp; &nbsp; $show = $showSecond;<?php endif; ?>&nbsp;<?php&nbsp; end for;?>

交互式爱情

尝试这个:<div class="fadeIn col-lg-12 col-sm-12 col-md-12" >&nbsp; &nbsp; <?php $loop = new WP_Query(array('post_type'=>'menu'));&nbsp; &nbsp; if($loop->have_posts()) :&nbsp; &nbsp; $i=0;&nbsp; &nbsp; ?>&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <?php while($loop->have_posts() ): $loop->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j = 0; ?>&nbsp; &nbsp; &nbsp; &nbsp; <ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while(the_repeater_field('item')):&nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ($j === 5): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul><ul style="width:50%" class="menu-list col-md-6 column&nbsp; <?php echo the_title(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j = 0; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li style="max-width:1000px;"><strong class="icon">&#x2022;</strong>&nbsp;<b><?php the_sub_field('heading'); ?></b><span class="price" >1.95</span><br><strong style="font-weight:400;margin-left:22px;">Soybean paste broth</strong></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $j++; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; endwhile;?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile;&nbsp; endif; ?>&nbsp; &nbsp; </div></div>
打开App,查看更多内容
随时随地看视频慕课网APP