Reputation: 23
I'm using ACF pro 5. I create a Repeater Field with two sub_field
, called sub_field_item
& sub_field_value
. I need a code which will hide if sub_field_value
is empty. Probably sub_field_item
contains text but if sub_field_value
empty, then it will hide both.
I tried with this code, but it's not working.
<?php
if( have_rows('myrepeater') )
{
$field_key = "field_5aa18d1bc322c"; //KEY for Repeater main field "myrepeater
$field = get_field_object($field_key);
foreach($field['value'] as $value)
{
if(!empty($value['sub_field_item']))
{
$not_empty = true;
break;
}
}
if($not_empty == true)
{
echo '<h2>' . $field['label'] . '</h2>';
}
while ( have_rows('sub_field_item') )
{
the_row();
$subfield = get_sub_field('sub_field_value');
if( !empty($subfield) )
{
echo '<b>' . $subfield . '</b>';
}
}
}
?>
Upvotes: 1
Views: 2889
Reputation: 1106
here is the magic code :
<?php
if(get_field('field_name')):
while(has_sub_field('field_name')):
if(get_sub_field('subfield_name')): ?>
<h2><?php echo get_sub_field('subfield_name'); ?></h2>
<?php endif;
endwhile;
endif;
Upvotes: 4