分析ACF/Wordress中中继器字段内的post对象



我试图从中继器字段内的post对象中获取标题等。

$classes = get_field('classes'); //repeater field containing a sub field named "class" (post object).
<?php foreach($classes as $class) : ?>
<?php 
echo $class; //returns post objects as arrays.
echo $class['title']; //returns nothing.
echo $class['post_title']; //returns nothing.
?>
<?php endforeach; ?>

返回内容:

[class] => WP_Post Object
(
[ID] => 57
[post_author] => 1
[post_date] => 2021-12-07 23:55:28
[post_date_gmt] => 2021-12-07 23:55:28
[post_content] => fffdfdf
[post_title] => testa
[post_excerpt] => dfdsgdsgf
etc.....
)

那么我该如何获得帖子标题等?

根据ACF文档,这就是如何循环通过Repeater字段并加载子字段值:ACF|Repeater

// Check rows exists.
if( have_rows('classes') ):
// Loop through rows.
while( have_rows('classes') ) : the_row();
// Load sub field value.
$product = get_sub_field('class');
// Access product info.
$product_id   = $product->get_id();
$product_name = $product->get_name();
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;

访问产品信息的方法列表。

最新更新