【问题标题】:ACF get data for all postsACF 获取所有帖子的数据
【发布时间】:2019-07-16 22:43:58
【问题描述】:
需要将每个帖子的所有 ACF 数据放入一个数组中
我试过了
$fields = get_field_objects();
foreach( $fields as $allData ){
echo $allData['label']." = ".$allData['value'];
echo "<br/>";
}
但它只给我当前帖子的数据。
【问题讨论】:
标签:
wordpress
advanced-custom-fields
【解决方案1】:
尝试在每个“帖子”中循环播放或将其他 CPT 添加到“post_type”
重要的是使用wp_reset_postdata(); after 将循环返回到当前帖子。
<?php
$wpQuery = new WP_Query( array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
) );
if( $wpQuery->have_posts() ) :
while( $wpQuery->have_posts() ) : $wpQuery->the_post();
$fields = get_field_objects();
foreach( $fields as $allData ){
echo $allData['label']." = ".$allData['value'];
echo "<br/>";
}
endwhile;
wp_reset_postdata();
else :
// empty
endif;