0

The way I am trying to set this up is that if a link has not been chosen then only the image will be displayed, but if a link is chosen as well as an image then I want the link to be wrapped around the image, here is an example of what I have at the moment:

<?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; ?>

The does not seem to be working correctly though as its rendering out about a hundred of these:

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

Don't suppose anyone knows what I have done wrong please?

  • why you are using while loop? – Anjum Sep 16 '13 at 21:10
  • I am trying to check if block_link exists and then block_image, is there a correct way to do this? – adam Sep 16 '13 at 21:16
0

use if instead of while because you want to compare if a field is exist or has some value, by using while you are looping trough and this loop can't stop until your php's execution time limit is ended, try this.

<?php if (get_field('block_repeater')) { ?>
<ul>
    <?php if (has_sub_field('block_repeater')) : ?>
        <li>
            <!-- problem -->
            <?php $block_link = get_sub_field('block_link');
            $block_image = get_sub_field('block_image');

            if ($block_image) { ?>
                <?php if ($block_link) { ?><a class="img" href="<?php the_sub_field('block_link'); ?>"><?php } ?>
                    <img src="<?php the_sub_field('block_image'); ?>" alt="" />
                    <span class="square-arrow"></span>
                <?php if ($block_link) { ?></a><?php } ?>
            <?php } ?>
        </li>
    <?php endif; ?>
</ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.