【问题标题】: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;

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 2019-05-18
    • 2016-12-29
    • 1970-01-01
    • 2021-12-10
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多