Home › Forums › General Issues › Hide Empty Group Field
I have a group which includes an image field. If set, the image should display as a background image, but if not, none of the html/css related to it should show but I can’t figure out how to set the if
for the sub field. Any ideas?
This is what I have so far.
<?php $servicel = get_field('large_service'); if( $servicel ): ?>
<div class="col-50">
<div class="promo" <?php if ( get_sub_field('background_image')): ?>style="background-image: url(<?php echo $servicel['background_image']['url']; ?>);"<?php endif; ?>>
Content
</div>
</div>
<?php endif; ?>
A group field is a repeater field that always has 1 row. if(have_rows('group_field'))
will always return true and get_field('group_field)
will always return a non-empty array. You actually need to check each sub field.
// loop
while (have_rows('large_service')) {
the_row();
if (get_sub_field('background_image')) {
$image = get_sub_field('background_image');
?>
<div class="col-50">
<div class="promo" style="background-image: url(<?php
echo $image['url']; ?>);">
Content
</div>
</div>
<?php
}
}
// array
$servicel = get_field('large_service');
if ($service1['background_image'])) {
?>
<div class="col-50">
<div class="promo" style="background-image: (<?php
echo $service1['backgroud_image']['url']; ?>);">
Content
</div>
</div>
<?php
}
Hello, I would need your help on something a bit similair. If “filters” is empty, hide <div class=”row spacer-bottom-sm”>, <div class=”col”> and <ul class=”list-labels d-none d-lg-flex”> — Is this possible?
<!-- Filters -->
<?php if( have_rows('portfolio') ): ?>
<div class="row spacer-bottom-sm">
<div class="col">
<ul class="list-labels d-none d-lg-flex">
<?php while( have_rows('portfolio') ): the_row(); ?>
<?php if( have_rows('filters') ): ?>
<li><button class="label active" data-filter="*">Tous</button></li>
<?php while( have_rows('filters') ): the_row();
$title = get_sub_field( 'title' );
$class = get_sub_field( 'class' );
?>
<li><button class="label" data-filter=".<?php echo $class; ?>"><?php echo $title; ?></button></li>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endif; ?>
I would use an output buffer https://www.php.net/manual/en/book.outcontrol.php
<!-- Filters -->
<?php
$has_filters = false;
ob_start();
if( have_rows('portfolio') ): ?>
<div class="row spacer-bottom-sm">
<div class="col">
<ul class="list-labels d-none d-lg-flex">
<?php while( have_rows('portfolio') ): the_row(); ?>
<?php if( have_rows('filters') ): ?>
<li><button class="label active" data-filter="*">Tous</button></li>
<?php while( have_rows('filters') ): the_row();
if (get_sub_field('title')) {
$has_fitlers = true;
$title = get_sub_field( 'title' );
$class = get_sub_field( 'class' );
?>
<li><button class="label" data-filter=".<?php echo $class; ?>"><?php echo $title; ?></button></li>
<?php
} // end if get sub field
?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endif;
$filters = ob_get_clean();
if ($has_filters) {
echo $filters;
}
?>
You’re a life saver, a small typo here: $has_fitlers
Corrected it, thanks a lot man! 😀
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.