Dear ACF users,
The last days i am trying to hide my <?php the_sub_field();?> when it is empty. I want this because i repeat the action many times and i dont use the division always.
The code is looking like:
<?php if( have_rows('instructie-afbeelding') ): ?>
<?php while ( have_rows('instructie-afbeelding') ) : the_row();?>
<div class="stappen-instructiescreens">
<h3><?php the_sub_field('stap-title');?></h3>
<img src="<?php the_sub_field('afbeelding');?>" style="max-width:100%;" width="auto" height="auto">
<div class="code-title"><h3><?php the_sub_field('code-title');?> </h3></div>
</div>
<!-- end afbeeldingen !-->
<div class="content-code">
<article>
<pre>
<?php the_sub_field('code-snippet');?>
</pre>
</article>
</div>
<?php endwhile; ?>
<?php endif; ?>
I tried some things but i cant get it working, i want to get the sub field ‘code-snippet’ on display:none when no value, so from the class:content-code it need to be removed when empty.
I hope someone can help me, thanks for reading.
You should use get_sub_field() to get the value into a variable, this way:
<?php
$style = "";
$code_snippet = get_sub_field('code-snippet');
if(empty($code_snippet)){
$style = 'style="display:none;"';
}
?>
<div class="content-code" <?php echo $style;?>>
<article>
<pre>
<?php echo $code_snippet; ?>
</pre>
</article>
</div>