我在循环中调用了这段代码,以获取图像元数据(标题/标题和描述)。它工作得很好,但只适用于循环中的第一个帖子。所以我认为它需要重置。从这里看我想wp_rest_postdata();
是重置查询的正确方法,但除了第一篇文章外,其他所有文章的输出仍然失败,它会将第一篇文章中的数据重复到其他每一篇文章中。
function get_image_metadata() {
$image_id = get_post_thumbnail_id($post->ID);
$args = array(
\'post_type\' => \'attachment\',
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'include\' => $image_id
);
$image_data = get_posts($args);
if ($image_data && isset($image_data[0])) {
$meta_output[\'title\'] = $image_data[0]->post_title;
$meta_output[\'caption\'] = $image_data[0]->post_excerpt;
$meta_output[\'description\'] = $image_data[0]->post_content;
}
//wp_reset_query();
wp_rest_postdata();
return $meta_output;
}