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>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.