From what I read, you read the URL/path to the post thumbnails is an array. We need to look at a couple of things here to make this as lean and predictable as possible
IMPORTANT CONSIDERATIONS
-
We do need some way to make sure that we get the latest three posts which actually have thumbnails to avoid unexpected output and bugs. It is always important to code with a mindset that my code will be hacked and it will fail and it will have bugs. Once you have that mindset, then you look at your code from a different angle, which make you to ask questions like: What if this specific poece of code returns an error, how will it affect my output, how will I handle such errors safely and reliable in an expected, predictable way without breaking something else on my site
-
We only really need the post ID’s from the three latest posts, so to save resources, why not only query for post ID’s
-
When you need lean, non-paginated, to the point queries like this, I always use
get_posts
as it breaks pagination legally. This makes your query faster, which really helps on sites with many posts. You can still useWP_Query
, but then you would need to remember to pass'no_found_rows' => true
to your query arguments (which is exactly whatget_posts
does btw). Another plus usingget_posts
is that it just return the$posts
property from theWP_Query
instance. But this is all just personal preference
THE CODE
I prefer to write functions for long pieces of code to keep my templates as simple and short as poosible. I can then just call my functions where I need them or even use do_action()
calls in my templates and then hook my functions to that specific action
Anyway, lets look at the function; (NOTE: The following is untested, needs PHP 5.4+ and canbe made more dynamic as needed)
function get_latest_post_thumbnails_urls()
{
// Set an empty variable which will hold our array of URL's
$output = [];
// Set our query args
$args = [
'posts_per_page' => 3,
'fields' => 'ids', // Only get post ID's
'meta_query' => [ // Get posts which has thumbnails only
[
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
]
],
// Any additional parameters you might need
];
$q = get_posts( $args );
// ALWAYS make sure we have posts, else return $output
if ( !$q )
return $output;
// Ok, we have posts, lets loop through them and create an array of URL's
foreach ( $q as $id )
$output[] = wp_get_attachment_url( get_post_thumbnail_id( $id ) );
// Return our array
return $output;
}
We can now use get_latest_post_thumbnails_urls();
anywhere where we need it.
USAGE
We need to remember, our function might return an empty array, or an array with 1, 2, or 3 URL’s, so we must always make sure about this before we try to use anything to avoid bugs and have unexpected failures
This is a probable safe usecase we can use
$urls = get_latest_post_thumbnails_urls();
// Make sure we do not have an empty array
if ( $urls ) {
foreach ( $urls as $url ) {
// Do something with your thumbnail url
echo $url . '</br>'; // For testing, remove this
}
}