The goal is to create a custom post (intervenant) with custom fields (job / image). (done)
Select this object in Post (done)
and display the content in a shortcode (in progress)
I did this Loom to show you what I did. The result in intervenant (The two picture with the lorem ipsum) is static for now.
This my code now, I only see the name and content of the custom post and no custom field 'job' or 'image' :
add_shortcode('display_acf_image', 'acf_image');
function acf_image() {
// HERE : HTML CSS PHP to display the content of the custom post.
$intervenant = get_field('intervenant');
debug($post); //print
if( $intervenant ): ?>
<h3><?php echo esc_html( $intervenant->post_title ); ?></h3>
<?php foreach( $intervenant as $post ):
setup_postdata($post); ?>
<h4><?php the_field( 'job' ); ?></h4>
<?php endforeach; ?>
<?php endif;
}
My code should work like this ?
This is the print_r of intervenant :
WP_Post Object
(
[ID] => 637
[post_author] => 2
[post_date] => 2021-06-19 16:37:23
[post_date_gmt] => 2021-06-19 14:37:23
[post_content] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a sodales arcu. Sed facilisis eros non vehicula pretium. Suspendisse et hendrerit arcu. Mauris imperdiet mauris nec sapien mollis, ut dictum nisl dignissim. Aliquam ultricies tincidunt hendrerit. Nulla facilisi. Aliquam non pulvinar velit, id tempor lectus. Nullam a libero non leo auctor gravida. Aenean hendrerit ut dolor consequat ullamcorper. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec volutpat, lacus vitae elementum maximus, velit velit eleifend libero, ac hendrerit orci dui ut libero. Donec lacus metus, dignissim sed lorem nec, luctus vehicula nisl. Duis efficitur turpis nec efficitur tempor.
[post_title] => Gregory House
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => gregory-house
[to_ping] =>
[pinged] =>
[post_modified] => 2021-06-28 09:40:21
[post_modified_gmt] => 2021-06-28 07:40:21
[post_content_filtered] =>
[post_parent] => 0
[guid] => https://healthevents.digiworks.fr/?post_type=seriestv&p=637
[menu_order] => 0
[post_type] => seriestv
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
Thx
EDIT1 : It's a beautiful print :)
function debug($print)
{
?>
<pre><?php
print_r($print);
?></pre><?php
}
I find the way to do it :
add_shortcode('display_acf_image', 'acf_image');
function acf_image()
{
$intervenant = get_field('intervenant');
if( $intervenant ):
global $post;
foreach( $intervenant as $post ):
setup_postdata($post);
$name = get_field( 'name' );
<span><?= $name ?></span>
endforeach;
endif;
}
I use setup_postdata() to have access to the custom post and custom fields of it.