无法从自定义帖子类型检索ACF值



我有一个自定义的帖子类型'fotos'与几个ACF字段连接到它。

在我的主页的while循环内,我正在从这个CPT检索最近的帖子。我想为每个帖子显示一个名为"clients"的ACF字段。$photo->ID正确返回CPT帖子的ID。

<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<!-- ... -->
<?php
$recent_photos = wp_get_recent_posts(array(
'numberposts' => '8',
'post_type' => 'fotos',
'post_status' => 'publish',
));
if ($recent_photos):
?>
<?php foreach ($recent_photos as $photo): ?>
<div class="item-grid--item">
<?php the_field('client', $photo->ID); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<!-- ... -->
<?php endwhile;
else:
_e('Sorry, no posts matched your criteria.', 'textdomain');
endif;
?>

但是,the_field、get_field、the_sub_field或get_sub_field绝对不返回任何值。会是什么呢?我检查过了,一切都是正确的!

wp_get_recent_posts的默认返回值类型是一个关联数组,而不是对象。

所以你可以改变wp_get_recent_posts的返回类型
$recent_photos = wp_get_recent_posts(array(
'numberposts' => '8',
'post_type' => 'fotos',
'post_status' => 'publish',
), object);

或者您可以在默认代码中更改获取ID。

<?php the_field('client', $photo['ID']); ?>

最新更新