Home › Forums › General Issues › Post object in Flexible Content
Hello friends,
I start with ACF, and I already have my first question.
I have a flexible content named “experiences”
In this flexible content, i have a layout named “agrement_du_centre” type : Post Object
I would like to collect the custom fields of this post
this should give a “Post object” in “Flexible Content”
<div class="experiences">
<?php
// Check value exists.
if( have_rows('experiences') ):
echo '<h2 data-fontsize="24" data-lineheight="27">Expériences</h2><table class="listexp"><tr class="title"><td class ="date-in">Du</td><td class ="date-out">Au</td><td class ="agrement">Agrément du centre</td><td class ="adresse">Adresse</td><td class ="notif">Notification</td></tr>';
// Loop through rows.
while ( have_rows('experiences') ) : the_row();
// Case: Paragraph layout.
if( get_row_layout() == 'experience' ):
$du = get_sub_field('date_entree');
$au = get_sub_field('date_sortie');
$ctrlid = get_sub_field('agrement_du_centre');
$notif = get_sub_field('notification_ch_ctrl');
echo '<tr><td class ="date-in">' . $du . '</td>
<td class ="date-out">' . $au . '</td>
<?php $post_object = get_field(' . $ctrlid . ');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<td class="agrement"><?php the_title(); ?><?php the_field('experiences_0_agrement_du_centre'); ?></td><td class="adresse"><?php the_field('cp_du_centre'); ?> <?php the_field('ville_du_centre'); ?></td>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<td class ="notif"><a href="' . $notif . '" target="_blank"><i class="fa fa-file-pdf-o"></i></a></td></tr>';
endif;
endwhile;
echo '</table>';
else:
echo '<h2 data-fontsize="24" data-lineheight="27">Expériences</h2><p><i>Aucune expérience</i></p>';
endif; ?>
</div>
But that does not work
You will tell me “obviously”. But I want to understand why ?
This is getting your post object field
$ctrlid = get_sub_field('agrement_du_centre');
What is this field set to return?
In any case this will not return anything because the value returned above is either an id or a post object.
$post_object = get_field(' . $ctrlid . ');
Taking a wild guess by looking at this since you named the value $ctrlid
that you are returning the post ID then you would get fields from this other post by using
get_field('field-name-on-other-post', $ctrlid);
on the other hand if $ctrlid
is a post object then you’d use
get_field('field-name-on-other-post', $ctrlid->ID);
Hi John,
Thanks for your help
My problem was general PHP, I did PHP in PHP.
So I opened and closed correctly my php tag
Thank you for helping me on the fact that I used the code of the post_object like in the documentation here
Thank you for telling me that I could use the get_field simply
<div class="experiences">
<?php
// Check value exists.
if( have_rows('experiences') ):
echo '<h2 data-fontsize="24" data-lineheight="27">Expériences</h2><table class="listexp"><tr class="title"><td class ="date-in">Du</td><td class ="date-out">Au</td><td class ="agrement">Agrément du centre</td><td class ="adresse">Adresse</td><td class ="notif">Notification</td></tr>';
// Loop through rows.
while ( have_rows('experiences') ) : the_row();
// Case: Paragraph layout.
if( get_row_layout() == 'experience' ):
$du = get_sub_field('date_entree');
$au = get_sub_field('date_sortie');
$ctrlid = get_sub_field('agrement_du_centre');
$notif = get_sub_field('notification_ch_ctrl'); ?>
<?php echo '<tr><td class ="date-in">' . $du . '</td>' ?>
<?php echo '<td class ="date-out">' . $au . '</td>' ?>
<?php echo '<td class ="agrement">' ?>
<?php echo get_the_title( $ctrlid ); ?>
<?php echo '</td>' ?>
<?php echo '<td class ="adresse">' ?>
<?php the_field('cp_du_centre', $ctrlid); ?> <?php the_field('ville_du_centre', $ctrlid); ?>
<?php echo '</td>' ?>
<?php echo '<td class ="notif"><a href="' . $notif . '" target="_blank"><i class="fa fa-file-pdf-o"></i></a></td></tr>'; ?>
<?php endif;
endwhile;
echo '</table>';
else:
echo '<h2 data-fontsize="24" data-lineheight="27">Nombre de VTP par année</h2><p><i>Aucune valeur</i></p>';
endif; ?>
</div>
In the input form in wp_admin : $ctrlid is a post selector that returns the id of the selected post.
This code could be light, I would take the time to heal my code later
The topic ‘Post object in Flexible Content’ is closed to new replies.
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.