ACF:在Post循环中无法从Relation-field Post对象获取标题

时间:2018-12-06 10:22:15

标签: php wordpress function loops advanced-custom-fields

我正在尝试使用ACF字段在管理页面内设置自定义列。该字段是一个关系字段,但是我不断出错。 我在functions.php中的代码:

other: Any?

与此相关的是以下错误消息:

  

注意:尝试获取非对象的属性“ post_title”

如果我做function my_product_columns($columns) { $columns = array( 'cb' => '<input type="checkbox" />', 'title' => 'Name', 'artist' => 'Artist', 'media' => 'Media', ); return $columns; } function my_product_artist_columns($column) { global $post; $post_id = $post->ID; if ($column == 'artist') { $artist_field = get_field( "product_artist", $post_id); echo $artist_field->post_title; //THIS IS THE PROBLEM! } else { echo ''; } } ,我会得到:

  

致命错误:未捕获错误:无法将WP_Post类型的对象用作数组中的

echo $artist_field['post_title'];给了我: WP_Post对象

print_r($artist_field);

是因为它是后循环中的一个后循环,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了答案:https://support.advancedcustomfields.com/forums/topic/use-post-object-in-a-loop-of-a-custom-post-type/

我的代码是:

if ($column == 'artist') {
            $post_object = get_field( "product_artist"); if( $post_object ) {   
            $post = $post_object; setup_postdata( $post );  //Call the post object Inside and work with
            the_title();
            $post = $post_id; setup_postdata( $post ); ////Call back the "parent" post datas
            } else {
                echo '';
            }
    }
    else {
         echo '';
    }