I am trying to display 6 random images from a gallery on page id 7 in the sidebar which is on every page of the website.
This is my code but I cannot get the thumbnail link to show!
$args = array( 'post_id' => '7', 'post_type' => 'attachment', 'numberposts' => 6, 'orderby' => 'rand');
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$attachment_id = $attachment();
print_r($attachment())
echo '<a href="' . wp_get_attachment_url() . '"><img src="' . wp_get_attachment_image_src( $attachment_id ) . '" /></a>';
}
}
Your query is wrong. You are calling
$attachment()
as and id (?!). When you do aprint_r
of$attachment
in the foreach loop, you can see that they are clearly objects. So you need to get the id from each object by referencing to the needed value with->
See here about OOP more.Also the wp_get_attachment_image_src() returns an array that contains
Since that is an array, I always check if it’s not empty (just to be safe).
After querying, always do
wp_reset_postdata();
.That’s it.
Explanation in a bit more detail
So what you are doing here is a simple query. You want to get posts, that match some parameters you’ve set in your
$args
array. You can do a lot with it. Clicky here.What that query does is, it returns posts that match your query parameters. And what it returns is an array of objects. So if you want to access every one of them, you need to loop through it. And for that there is
foreach
loop.Foreach will go through every item in your array, and you’ll be able to access it as
$attachment
(in your case). You can loop any kind of array. A simple array, a multidimensional array, array containing objects etc.So, we’ve said that your query will return a list of objects (array). Inside your foreach loop
$attachment
is that single object. In general object is a thing that can contain a lot of things. Properties that describe that object, methods (functions in the object) that will make that object do something if you want it to, etc.Ours object is relatively simple. It contains properties like
ID, post_author,
and more. Basically, all you need to find information about your post.post_name, post_type, post_title
In our foreach, as mentioned, you are referencing a single object with
$attachment
, and you can get the value of it’s various properties by referencing to them likethis will return the value of the
ID
property. If you want to get the post title, you’ll referenceAnd so on.
So we’ve assigned that ID to the variable called
$attachment_id
. Now we can freely use it as we wish with various wordpress made functions likewp_get_attachment_image_src()
for instance.You need to be aware what you get out of queries and in loops, so it’s always good idea, when you’re developing stuff, to
print_r()
what you get out.Also, have in mind that
foreach
will loop through posts one by one. So even if yourprint_r($attachment)
returns bunch of posts, it only means that it did that multiple times (well in your case 6 times, since your query returns only 6 posts) and just printed it out. It will be all ‘glued’ together so you’ll think: But wait, this foreach returns tons of stuff. How to get only single stuff?But just remember that the code inside the
foreach
loop refers to a single instance of your array, and that foreach will go like:And that’s how foreach works, so don’t be afraid of it 😀
I’ve gone a bit off track, but I hope this helps someone in the future as well 🙂