Home › Forums › Add-ons › Options Page › Rows showing even when no data.
Can someone please look at this code and let me know where I am going wrong? I have been using this code for at least two years and for some reason, it has just stopped working properly.
Here is my set up in ACF Pro (latest version):
I have a Group and within the group, 3 sub-fields (see image for the names, etc). These are set to show on both post type page and my option pages (which are set up in functions.php as option, option2, option3).
My issue is that my div cta_wrap is showing up even when there is no content entered in that custom field on the page. I’ve gone one step further and even made sure that none of the pages have anything in that custom field. I am sure it’s something simple but please, help my tired brain!
<?php global $post; if ( get_field('page_heading', $post->ID ) ): ?>
<div id="cta_wrap" />
<?php echo $post->ID; ?>
</div>
<?php endif; // endif is home page ?>
I have also tried
<?php global $post; if ( have_rows('page_heading', $post->ID ) ): while(have_rows('page_heading', $post->ID) ): the_row(); ?>
<div id="cta_wrap2" />
<?php echo $post->ID; ?>
</div>
<?php endwhile; endif; // endif is home page ?>
and
<?php global $post; if ( have_rows('page_heading', $post->ID ) ): the_row(); ?>
<div id="cta_wrap2" />
<?php echo $post->ID; ?>
</div>
<?php endif; // endif is home page ?>
NOTE: Here is a screenshot of my setup
Your “page_heading” field is a group field. A group field always has one row even when there is no content entered for the sub fields.
if (have_rows('group_field')) {
// always true
while (have_rows('group_field')) {
// always happens once
}
}
To check for content you must check all of the sub field that need to have content.
if (get_sub_field('sub_field_1') && get_sub_field('sub_field_2') && get_sub_field('sub_field_3')) {
// do something
}
This also always returns an array with one row
get_field('page_heading', $post->ID )
returns
array(
'sub_field_1' => '',
'sub_field_2' => '',
'sub_field_3' => ''
)
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.