Support

Account

Home Forums ACF PRO Only show containing div if sub field has content in

Solved

Only show containing div if sub field has content in

  • Hello,

    I have been trying to find a code example for this but am having trouble find what I need. My Code to display a repeater filed with 2 subfields is:

    <div class=”main-content”>

    <?php if( have_rows(‘section_about_yoga’) ): ?>
    <?php
    // loop through rows (parent repeater)
    while( have_rows(‘section_about_yoga’) ): the_row(); ?>
    <div class=”yoga-section clearfix”>
    <div class=”col-md-7 margr”><?php the_sub_field(‘text_about_yoga’); ?></div>
    <div class=”col-md-5 sidebar”><div class=”bordered-box”><?php the_sub_field(‘sidebar_yoga_info’); ?></div></div>
    </div>

    <?php endwhile; ?>

    <?php endif; ?>

    </div>

    `

    I have styling on the containing div: <div class="bordered-box"> – but I don’t want this div to display if there is not content in it – as i get an empty bordered box.
    How can i add another if / while the subfield has content, around this div?

    Thanks in advance!
    Lucy

  • 
    <div class="main-content">
    
    <?php if( have_rows('section_about_yoga') ): ?>
    <?php
    // loop through rows (parent repeater)
    while( have_rows('section_about_yoga') ): the_row(); ?>
    <div class="yoga-section clearfix">
    <div class="col-md-7 margr"><?php the_sub_field('text_about_yoga'); ?></div>
    <div class="col-md-5 sidebar">
    <?php 
    	if (get_field('sidebar_yoga_info')) {
    		?>
    			<div class="bordered-box"><?php the_sub_field('sidebar_yoga_info'); ?></div>
    		<?php 
    	}
    ?>
    </div>
    </div>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    
    </div>
    
  • Hello, thank for your suggestion – but this actually makes none of the sidebar display – even the ones with content in.

    Any alternative suggestions?

    Thanks

  • That’s because I used get_field() instead of get_sub_field() in my example.

    
    <div class="main-content">
    
    <?php if( have_rows('section_about_yoga') ): ?>
    <?php
    // loop through rows (parent repeater)
    while( have_rows('section_about_yoga') ): the_row(); ?>
    <div class="yoga-section clearfix">
    <div class="col-md-7 margr"><?php the_sub_field('text_about_yoga'); ?></div>
    <div class="col-md-5 sidebar">
    <?php 
    	if (get_sub_field('sidebar_yoga_info')) {
    		?>
    			<div class="bordered-box"><?php the_sub_field('sidebar_yoga_info'); ?></div>
    		<?php 
    	}
    ?>
    </div>
    </div>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    
    </div>
    
  • Thank you very much, this now works,
    Lucy

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

The topic ‘Only show containing div if sub field has content in’ is closed to new replies.