通过带有 ACF 的自定义字段从自定义 post 获取自定义字段

Get custom field from a custom post throught a custom fields with ACF

目标是创建一个带有自定义字段(工作/图像)的自定义 post(干预)。 (完成)

Select 这个对象在 Post(完成)

并在短代码中显示内容(进行中)

我这样做 Loom 是为了向您展示我所做的。干预的结果(带有 lorem ipsum 的两张图片)目前是静态的。

现在这是我的代码,我只看到自定义 post 的名称和内容,没有自定义字段 'job' 或 '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;

}

我的代码应该像这样工作?

这是干预的print_r :

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
)

感谢

编辑1: 印刷精美:)

function debug($print)
{
    ?>
    <pre><?php
    print_r($print);
    ?></pre><?php
}

我找到方法了:

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;



}

我使用 setup_postdata() 来访问它的自定义 post 和自定义字段。