Support

Account

Home Forums Add-ons Repeater Field Question about conditional branch with Repeater Field

Solved

Question about conditional branch with Repeater Field

  • Hello,I’m having difficuly in codeing with ACF and Repeater Field.

    I want to hide ‘<div class="box">~~~</div>‘,
    when ‘gallery_photo’ are not input.

    But my code is not work…

    Please tell me good way.

    <?php if(get_field('gallery')): ?>
    <div class="box">
    <h3><img src="images/images.png" /></h3>
    <div id="gallery" class="ad-gallery">
    <div class="ad-image-wrapper"></div>
    <div class="ad-nav">
    <div class="ad-thumbs">
    <ul class="ad-thumb-list">
    <?php while(has_sub_field('gallery')): ?>
    <?php
    $image = wp_get_attachment_image_src(get_sub_field('gallery_photo'), 'none');
    $thumb = wp_get_attachment_image_src(get_sub_field('gallery_photo'), 'thumbnail');
    ?>
    <?php if(!empty($image)): ?><li><a href="<?php echo $image[0] ?>"><img src="<?php echo $thumb[0] ?>"></a></li><?php endif; ?>
    <?php endwhile; ?>
    </ul>
    </div>
    </div>
    </div>
    </div>
    <?php endif; ?>

    At last I’m sorry for my bad English…
    I hope you can understand what I want to tell.

  • Hi @k2b8692

    Your code shows that you already have an if statement warping the .box div.

    Have you tested your code, or are you just asking the question.

    Have you tried debugging the value to find out why you if statement is / isn’t working?

  • Hi,elliot.

    I couldn’t get result with what I posted.
    But finally I could get good result with below code.

    Thank you for your comment.

    <?php
    $gallery = get_field('gallery' ); // get all the rows
    $gallery_items = $gallery[0]; // get the first row
    $gallery_item = $gallery_items['gallery_photo']; // get the sub field value 
    if($gallery_item):
    ?>
    <div class="box">
    <h3><img src="images/images.png" /></h3>
    <div id="gallery" class="ad-gallery">
    <div class="ad-image-wrapper"></div>
    <div class="ad-nav">
    <div class="ad-thumbs">
    <ul class="ad-thumb-list">
    <?php while(has_sub_field('gallery')): ?>
    <?php
    $image = wp_get_attachment_image_src(get_sub_field('gallery_photo'), 'large');
    $thumb = wp_get_attachment_image_src(get_sub_field('gallery_photo'), 'thumbnail');
    ?>
    <li><a href="<?php echo $image[0] ?>"><img src="<?php echo $thumb[0] ?>"></a></li>
    <?php endwhile; ?>
    </ul>
    </div>
    </div>
    </div>
    </div>
    <?php endif; ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Question about conditional branch with Repeater Field’ is closed to new replies.