Home Forums Add-ons Repeater Field Hide Empty Repeater Field Images


Hide Empty Repeater Field Images

  • Hi there

    I’m trying to get a food menu to work, but I’ve hit a snag. I’m not great with PHP but from the ACF repeater documentation I managed to piece this together.

    if ( have_rows('food_section') ):
    while ( have_rows('food_section') ): the_row(); ?>
    	<h2><?php the_sub_field('food_section_title'); ?></h2>
    	<?php if ( have_rows('food_section_items') ): ?>
    		<?php while ( have_rows('food_section_items') ): the_row(); ?>
    			<div><?php the_sub_field('dish_name'); ?></div>
    			<div><?php the_sub_field('dish_price'); ?></div>
    			<div><?php the_sub_field('dish_description'); ?></div>
    			<div><img src="image1.jpg"><?php the_sub_field('carnivore_option'); ?></div>
    			<div><img src="image1.jpg"><?php the_sub_field('dairy_option'); ?></div>
    		<?php endwhile; ?>
    	<?php endif; ?>
    	<?php endwhile;
    endif; ?>

    The issue I have is that the last two options of ‘carnivore_option’ and ‘dairy_option’ need to have an icon (key) next to them, but sometimes there are no dairy or carnivore options and I need them not to show if there is not text in those text fields.

    Any help would be greatly appreciated! I’ve been at this for a couple of days and whilst I’m sure it’s simple enough, I’m primarily a designer and not great with code.

  • Hi @lukeshield

    You should be able to do it like this:

    <?php if( get_sub_field('carnivore_option') ){ ?>
        <div><img src="image1.jpg"><?php the_sub_field('carnivore_option'); ?></div>
    <?php } ?>
    <?php if( get_sub_field('dairy_option') ){ ?>
        <div><img src="image1.jpg"><?php the_sub_field('dairy_option'); ?></div>
    <?php } ?>

    I hope this helps 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.