高级自定义字段 - 如果/ while字段问题

时间:2013-09-16 21:06:12

标签: wordpress

我试图设置它的方式是,如果没有选择链接,那么只会显示图像,但是如果选择了链接以及图像,那么我希望链接被包裹在图片,这是我目前所拥有的一个例子:

<?php if(get_field('block_repeater')): ?>
     <ul>

         <?php while(has_sub_field('block_repeater')): ?>
         <li>
            <!-- problem -->
             <?php if( get_sub_field('block_link') ): ?>

                 <?php while(has_sub_field('block_image')): ?>
                    <a class="img" href="<?php the_sub_field('block_link'); ?>">
                         <img src="<?php the_sub_field('block_image'); ?>" alt="" />
                         <span class="square-arrow"></span>
                     </a>
                 <?php endwhile; ?>

             <?php endif; ?>
            <!--//problem -->

             <?php if( get_sub_field('block_image') ): ?>
                 <img src="<?php the_sub_field('block_image'); ?>" alt="" />
             <?php endif; ?>
         </li>
         <?php endwhile; ?>

     </ul>
<?php endif; ?>

这似乎没有正常工作,因为它渲染了大约一百个:

<a class="img" href="">
<img alt="" src="">
<span class="square-arrow"></span>
</a>

不要以为有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是因为你正在做一个循环并在html循环之间保持while块,所以这个块被打印直到循环结束。

<?php while(has_sub_field('block_image')): ?>
    <!--
        This part will be printed as long as 
        has_sub_field('block_image') returns true
    -->
<?php endwhile; ?>

如果你想只打印一次该部分,那么删除循环,只需注释掉这些部分,如

<?php //while(has_sub_field('block_image')): ?>
    <!-- keep everything here as it is -->
<?php //endwhile; ?>
相关问题