【发布时间】:2015-03-06 13:48:54
【问题描述】:
我有一个 PHP while 循环,我试图在它之外回显 $child_i 的内容,我该怎么做?
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- Trying to echo $child_i on the line below -->
<div class="responsive-accordion-panel <?php echo($child_i); ?>">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
<?php $child_i++; endwhile; ?>
</div>
<?php endif; //if( get_sub_field('items') ): ?>
</div>
</li>
【问题讨论】:
-
$child_i 值将继续存在于循环之外,因此您可以将其输出到那里。 (Endwhile 是 PHP 的替代语法,表示 while 循环在其上方一点开始的结束)
-
@GauravDave 请read docs
-
@GauravDave 它结束了 while 循环...
-
谢谢大家,endwhile我没用过。现在,大家会解决@egr103的问题吗
-
使用旧的卷曲 (
{}) 比使用:和endwhile更好。代码编辑器不支持这种语法,这使得调试和阅读变得困难。所有代码编辑器都支持 curlies,这让调试变得轻而易举:-)
标签: php wordpress while-loop advanced-custom-fields