chiconerd
Member
Posted 3 years ago #
Hi, I am having some troubles with this function.
I have a index page that only show the post, i wanted to show an image also so i added an image form the admin panel.
when I use $Img = wp_get_attachment_image($post->ID); nothing happens Img is empty.
however, if I use the_content() in the loop it shows the image.
so i know the image is working, the question is why can't i retrieve it individual?
i have also used <?php $image = wp_get_attachment_image_src($post->ID); ?> with the same results.
Depends on the version of WP you are using.
My function for that looks like the following:
//get the post attachments
$attachments = get_children($att_array);
//make sure there are attachments
if (is_array($attachments)){
//loop through them
foreach($attachments as $att){
//find the one we want based on its characteristics
if ( $att->menu_order == 0){
$image_src_array = wp_get_attachment_image_src($att->ID, $size);
//get url - 0 - url; 1 and 2 are the x and y dimensions
$url = $image_src_array[0];
$caption = $att->post_excerpt;
}
}
}
If you check ioni2 you would see it in action
chiconerd
Member
Posted 3 years ago #
I am using the latest virsion 2.8.
and that did not work, it says missing files.
i went back and used the content to make sure the files are there, and they are.