When using get_posts()
you don’t actually setup post data unless you do so explicitly, via setup_postdata()
inside your (usually a foreach
) loop.
So, since you’re not setting up post data, you shouldn’t need to reset post data.
but still the output fails for all but the first post
I think that’s because you’re calling the global $post
inside your function, but aren’t globalizing it first:
$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);
Try adding global $post;
before using $post->ID
:
global $post;
$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 that’s not the problem, can you clarify:
- What data you get from
$iamge_data
- The context in which you are calling the function