For 循环在 Wordpress 中不显示任何结果

For loop show nothing any result in Wordpress

我正在 Wordpress 中开发自己的主题,并使用高级自定义字段在 6 个小部分中显示 header 和文本。

我把它们命名为text_header_1,然后一直增加到6个。

所以我想我可以循环出正确的结果,但它不起作用。不知道普通的 for loops 能不能用 Wordpress 做?

无论如何,这是我失败的代码:

            <div class="media">


            <?php

            for($i = 1; $i > 7; $i++) {

                $numberHeader = 'text_header_' . $i ;
                $numberText = 'text_' . $i;

                $textHeader = get_field($numberHeader);
                $text = get_field($numberText);

            ?>

                <div class="pull-left">
                        <span class="fa-stack fa-2x">
                              <i class="fa fa-circle fa-stack-2x text-primary"></i>
                              <i class="fa fa-tree fa-stack-1x fa-inverse"></i>
                        </span>
                </div>
                <div class="media-body">

                    <h4 class="media-heading"><?php echo $textHeader; ?></h4>
                    <p><?php echo $text; ?></p>
                </div>

            <?php } ?>

        </div>

这样做的结果是什么都没有。我没有收到任何错误,只是一个空 div。 除了 for 循环,我还需要使用其他东西吗?也许一会儿?如何?

你的循环永远不会执行,因为你正在检查 $i 是否大于 7,而它永远不会是因为它从 1 开始。改变这个:

for($i = 1; $i > 7; $i++) {

对此:

for($i = 1; $i < 7; $i++) {