【发布时间】:2021-09-14 10:06:34
【问题描述】:
除类别名称和类别链接外,所有查询都有效,如果我使用“get_the_category()”,它会显示一些我不需要的东西,例如“category_count”、“category_description”等。
我需要知道如何按帖子类别分隔 jsons
这是我的代码
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
$posts = array();
while( $query->have_posts() ) : $query->the_post();
$posts[] = array(
'id' => get_the_ID(),
'date' => get_the_date(),
'title' => get_the_title(),
'link' => get_the_permalink(),
'image' => get_the_post_thumbnail_url(),
'category' => get_the_category ( 'cat_name' ),
'categoryLink' => get_category_link ( $category_id )
);
endwhile;
wp_reset_query();
$data = json_encode($posts);
$upload_dir = wp_get_upload_dir();
$file_name = date('Y-m-d') . '.json';
$save_path = $upload_dir['basedir'] . '/' . $file_name;
$f = fopen( $save_path , "w" );
fwrite($f , $data);
fclose($f);
}
add_action( 'save_post', 'export_posts_in_json' ); ```
【问题讨论】: