Tas*_*sos 3 wordpress advanced-custom-fields
我尝试从 Wordpress 中的自定义帖子类型获取所有帖子,并在结果中包含高级自定义字段 (ACF),以便生成包含数据的 JSON 文件。
$query = new WP_Query(array(
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => -1,
));
echo "var json=". json_encode($query->get_posts());
Run Code Online (Sandbox Code Playgroud)
使用简单的方法WP_Query,不包含 ACF 数据,我必须迭代结果并手动一一获取所有 ACF。有什么办法可以将它们包含在原始WP_Query结果中吗?
这就是我的做法。
将您想要的任何内容推送到数组并对其进行编码。
<?php
$array = array();
$args = array(
'post_type' => 'resources',
'post_status' => array( 'publish' ),
'nopaging' => true,
'posts_per_page' => '-1',
'order' => 'ASC',
'orderby' => 'ID',
);
$queryResults = new WP_Query( $args );
if ( $queryResults->have_posts() ) {
$counter = 0;
while ( $queryResults->have_posts() ) {
$queryResults->the_post();
$array[ $counter ][ 'ID' ] = get_the_ID();
$array[ $counter ][ 'name' ] = get_the_title();
$array[ $counter ][ 'thumbnailURL' ] = get_the_post_thumbnail_url();
$array[ $counter ][ 'place' ] = get_field( 'resource_location' );
//etc etc etc
$counter++;
}
$jasoned = json_encode( $array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
echo $jasoned;
} else {
//nothing found
}
wp_reset_postdata();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12689 次 |
| 最近记录: |