在 ACF/Wordress 中解析 post object 内的转发器字段
Parse post object inside repeater field in ACF/Wordress
我正在尝试从转发器字段内的 post object 获取标题等。
$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.....
)
那么我如何获得 post 标题等?
根据 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;
我正在尝试从转发器字段内的 post object 获取标题等。
$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.....
)
那么我如何获得 post 标题等?
根据 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;