get_the_date
depends on the global $post
variable. It is a bit hard to tell that that is the case but if you look at the source for get_the_date
you’ll see that it uses get_post
and it assumes $post
if no other parameters are given.
Your code never sets that global $post
variable except for when your WP_Query
initially runs. You need to refactor. I would return the post objects from get_images_from_media_library
and construct a more standard Loop to process them.
I think this should be better.
function get_images_from_media_library() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'order' => 'DESC'
);
$query_images = new WP_Query( $args );
return $query_images;
}
function display_images_from_media_library() {
$imgs = get_images_from_media_library();
$html="<div id="media-gallery">";
global $post;
if ($imgs->have_posts()) {
while($imgs->have_posts()) {
$imgs->the_post();
$html .= '<img src="' . $post->guid . '" alt="" />';
$html .= get_the_date('F j, Y');
}
}
$html .= '</div>';
return $html;
}
echo display_images_from_media_library();