Support

Account

Home Forums Add-ons Repeater Field Hide the title if the repeater's subfields are empty

Solved

Hide the title if the repeater's subfields are empty

  • Hello,

    Can you help me, how to hide the title if the repeater’s subfields are empty in basic loop?

    <?php if( have_rows('repeater_field_name') ): ?>
    <h2><?php _e('Title');?></h2> //need to hide this if subfield is empty
    <ul>
    <?php while ( have_rows('repeater_field_name') ) : the_row(); ?>
    <li><?php the_sub_field('repeater_subfield_name'); ?></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    Thank you so much

  • No guarantees, I haven’t actually tested it. LOL πŸ˜‰

    <?php
     if( have_rows('repeater_field_name') ):
      $repeater_subfield_name = get_sub_field('repeater_subfield_name');
       if( !empty($repeater_subfield_name) ):
    ?>
    <h2><?php _e('Title');?></h2> //need to hide this if subfield is empty
    <?php endif; ?>
    <ul>
    <?php while ( have_rows('repeater_field_name') ) : the_row(); ?>
    <li><?php echo $repeater_subfield_name ?></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
  • Sorry,
    title is hidden even if the subfields are not empty. Any ideas?

    Thanks a lot.

  • Hmmm, not the results I was expecting. A white screen maybe, but not that. πŸ˜›

    Let me* setup a test locally, and I will figure it out. πŸ™‚

  • Actually, what is

    <?php _e('Title');?>

    ?

    I, for some odd reason just assumed it was part of the same repeater field. Is it? If, not, where is that “Title” coming from?

  • Hello,
    “Title” is written in code but it is the same as label of repeater field.

  • If title is a sub field this should work. If it’s not a sub field, change it to get_field(‘title’)

    <?php
     if( have_rows('repeater_field_name') ):
      $title = get_sub_field('title');
      $repeater_subfield_name = get_sub_field('repeater_subfield_name');
       if( !empty($repeater_subfield_name) ):
    ?>
    <h2><?php echo $title ?></h2> // need to hide this if subfield is empty
    <?php endif; ?>
    <ul>
     <?php while ( have_rows('repeater_field_name') ) : the_row(); ?>
      <li><?php echo $repeater_subfield_name ?></li>
     <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    Hope it works. πŸ™‚

  • Uff,
    i tried, but no success.

    I’ll try to explain it on example again. The result is:

    <h2>Title</h2>
    
    <ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
    </ul>

    If the “li” (= repeater sub field) not exists, list not exists, “h2” (= repeater field label) is not displayed. i will be greatfull for any help with this lines of code.

  • Hello, this solved my problem:

     
    <?php
    if( have_rows('repeater_field_name') )
    {
    	$field_key = "repeater_field_number";
    	$field = get_field_object($field_key);
    
    	foreach($field['value'] as $value)
    	{
    	if(!empty($value['repeater_subfield_name']))
    		{
    		$not_empty = true;
    		break;
    		}
    	}
    
    	if($not_empty == true)
    	{
    	echo '<h2>' . $field['label'] . '</h2>';
    	}
    
    	echo '<ul>';
    	while ( have_rows('repeater_field_name') )
    	{		 					
    	the_row();
    	$subfield = get_sub_field('repeater_subfield_name');
    				
    		if( !empty($subfield) )
    		{
    		echo '<li>' . $subfield . '</li>';
    		}
    	}
    	echo '</ul>';
    }
    ?>
    
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.