Home › Forums › Add-ons › Flexible Content Field › Flexible content with post object – fields are missing
I have a field group with different fields which are assigned to a custom post type via post object.
profile_image (Image)
profile_function (Repeater)
profile_mail (Mail)
profile_phone (Phone)
These fields are output as follows:
<?php
$management = get_field('staff');
if ($management):
foreach ($management as $employee):
setup_postdata($employee);
?>
<div class="section__column--lg-4">
<div class="profile">
<div class="profile__image">
<img src="<?php echo get_field('profile_image', $employee->ID); ?>">
</div>
<div class="profile__body">
<div class="profile__name">
<?php $profile_title = get_the_title($employee); ?>
<strong><?php echo $profile_title; ?></strong>
</div>
<?php if (have_rows('profile_function', $employee->ID)): ?>
<div class="profile__function">
<ul class="profile__list">
<?php while (have_rows('profile_function', $employee->ID)) : the_row(); ?>
<li class="profile__item">
<?php echo get_sub_field('profile_function_entry', $employee->ID); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php if (get_field('profile_mail')): ?>
<div class="profile__mail">
<a class="profile__link">
<?php echo get_field('profile_mail', $employee->ID); ?>
</a>
</div>
<?php endif; ?>
<?php if (get_field('profile_phone')): ?>
<div class="profile__phone">
<?php echo get_field('profile_phone', $employee->ID); ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have created a new field group with flexible content which should output these fields as Post object. The problem is that only profile_image and profile_name and profile_function are displayed. profile_mail and profile_phone are missing from the output. What am I doing wrong?
<?php if(have_rows('management_block')): ?>
<?php while( have_rows('management_block')): the_row(); ?>
<?php if(get_row_layout() == 'management_entry'): ?>
<?php
$management = get_sub_field('management_profile');
if ($management):
foreach ($management as $employee):
setup_postdata($employee);
?>
<div class="section__column--lg-4">
<div class="profile">
<div class="profile__image">
<img src="<?php echo get_field('profile_image', $employee->ID); ?>">
</div>
<div class="profile__body">
<div class="profile__name">
<?php $profile_title = get_the_title($employee); ?>
<strong><?php echo $profile_title; ?></strong>
</div>
<?php if (have_rows('profile_function', $employee->ID)): ?>
<div class="profile__function">
<ul class="profile__list">
<?php while (have_rows('profile_function', $employee->ID)) : the_row(); ?>
<li class="profile__item">
<?php echo get_sub_field('profile_function_entry', $employee->ID); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php if (get_field('profile_mail')): ?>
<div class="profile__mail">
<a href="mailto:<?php echo get_field('profile_mail'); ?>" class="profile__link">
<?php echo get_field('profile_mail', $employee->ID); ?>
</a>
</div>
<?php endif; ?>
<?php if (get_field('profile_phone')): ?>
<div class="profile__phone">
<?php echo get_field('profile_phone', $employee->ID); ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
How does the second code snippet relate to the first? When/Where is the second called?
The first code snippet is in single.php and indicates the employee that was entered in the post. The second code snippet displays in a separate page template only the selected employees, which are selected by post object.
If I omit the if(get_field) statement for profile_phone and profile_mail, it works because the entries are always given in each case. But I don’t understand why nothing is displayed if the if(get_field) statement exists.
the difference is here
<?php if (get_field('profile_mail')): ?>
and
<?php echo get_field('profile_phone', $employee->ID); ?>
the firest needs to match the second.
Also, since you are using $employee->ID
to get field values you do not need setup_postdata($employee);
Using this means that values are set up in the global $post. See the example of using setup_postdata in the relationship field doc
Oh man, yes, that’s it.
<?php if(get_field('profile_mail', $employee->ID)): ?>
<div class="profile__mail">
<a href="mailto:<?php echo get_field('profile_mail', $employee->ID); ?>" class="profile__link">
<?php echo get_field('profile_mail', $employee->ID); ?>
</a>
</div>
<?php endif; ?>
Thanks a lot.
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.