Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Image Link Wordpress Outside of Loop WordPress

  • SOLVED

Hello All,

I am getting all images attached to a post using the following code:

<?php
$attachments = get_children(array('post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'));

foreach($attachments as $att_id => $attachment) {
$full_img_url = wp_get_attachment_url($attachment->ID);
$split_pos = strpos($full_img_url, 'wp-content');
$split_len = (strlen($full_img_url) - $split_pos);
$abs_img_url = substr($full_img_url, $split_pos, $split_len);
$full_info = @getimagesize(ABSPATH.$abs_img_url);

$thumb_width = $full_info[0];



echo '<div class="b-image">
<img src="'.$full_img_url.'" alt="'.$attachment->post_title.' Image" width="'.$full_info[0].'" height="'.$full_info[1].'">
</div>';
}?>



It works fine. The problem and what I am asking is if it is possible to the images link aswell.

I am <strong>not</strong> trying to get the attachments src. I am looking for the manual link you can enter in the backend when you upload an image. I effectively want the image to link to an external page and have to get that url first. That's where I'm stuck and what I need your help on.

If you need any further information or code please let me know and I will provide this to you asap.

Thank you.

Answers (2)

2011-06-18

Maor Barazany answers:

When you upload an image you cannot enter a manual link, the url field show the url of the uploaded image and it is a disabled field you cannot edit, and points to the image src.

You can use, for example, the [[LINK href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBkQFjAA&url=http%3A%2F%2Fwordpress.org%2Fextend%2Fplugins%2Ffile-gallery%2F&ei=Kln9Tc_MK9S88gOl24HVCQ&usg=AFQjCNGPbI8E87b6Q8W2mVM_tfa3j8pfmA"]]File Gallery[[/LINK]] plugin, which lets you add custom (meta) fields to uploaded attachments and then you can retrieve these fields in your code.


Maor Barazany comments:

If you meant the link you can enter when you attach an image inside a post, well, this link is not being saved as data of the image itself, but it is a regular anchor inserted part of the post's content, and of course it can not be retrieved by any of the attachment data.
As I've written, you can use the File Gallery and you'll be able to add meta fields to attachments.


Mathias Vagni comments:

Thank you. I am awarding you for your neat and precise answer.

2011-06-19

Christianto answers:

Hi Mathias Vagni ,

If the images attach is display on post content, I create function that find the link and match the src of attachment image (save on $full_img_url) and src of the image on post content.

If the function found the link, and the src matched then it will display the image wrapped with the link.

This function also works if you use thumbnail/medium size instead of full size image on your post.

function return_image_data($id = false, $post_type){

global $post;

if($id){

if($post_type == 'page'){
$p = get_page($id);
} elseif($post_type == 'post'){
$p = get_post($id);
}
$postcontent = $p->post_content;

} else {

$postcontent = $post->post_content;

}

$output_a = preg_match_all('/<a+.href=[\'"]([^\'"]+)[\'"].*><img.+src=[\'"]([^\'"]+)[\'"].*><\/a>/i', $postcontent, $matches_a_img);

if(is_array($matches_a_img)){

return $matches_a_img;

} else {

return false;

}

}

// passing the function post id (<em>int</em>) and post type (post/page) (<em>string</em>) of the post..
$thumb_url = return_image_data(65,'post');

// clean the src url if this is not full size aka thumnail/medium size..
$clean_index = 0;
foreach ($thumb_url[2] as $src_url_raw){
$src_url_clean = preg_replace('/\-(\d+)x(\d+)/i','',$src_url_raw);
$thumb_url[2][$clean_index] = $src_url_clean;
$clean_index++;
}

$attachments = get_children(
array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID')
);
foreach($attachments as $att_id => $attachment) {

$full_img_url = wp_get_attachment_url($attachment->ID);

$split_pos = strpos($full_img_url, 'wp-content');
$split_len = (strlen($full_img_url) - $split_pos);
$abs_img_url = substr($full_img_url, $split_pos, $split_len);

$full_info = @getimagesize(ABSPATH.$abs_img_url);

$thumb_width = $full_info[0];

if(in_array($full_img_url, $thumb_url[2])){

$index = 0;
foreach ($thumb_url[2] as $src_url){

if( $src_url == $full_img_url ){
$link = $thumb_url[1][$index];
echo '<div class="b-image">
<a href="'.$link.'"><img src="'.$full_img_url.'" alt="'.$attachment->post_title.' Image" width="'.$full_info[0].'" height="'.$full_info[1].'"></a>
</div>';
}

$index++;
}

} else {

echo '<div class="b-image">
<img src="'.$full_img_url.'" alt="'.$attachment->post_title.' Image" width="'.$full_info[0].'" height="'.$full_info[1].'">
</div>';
}
}


in the function you will see return_image_href() function, if you use this outside the loops you have to supply it with post ID and post type..

// passing the function post id (<em><em>int</em></em>) and post type (post/page) (<em>string</em>) of the post..
$thumb_url = return_image_data($postID, $post_type);


Let me know if this work on your site..
Thanks


Mathias Vagni comments:

Thank you very much for your help but unfortunately this was not was I was looking for and Maor Barazany had allready answered my question. Thank you anayways.