I have uploaded a bunch of images to a post and set the 'custom url' for each of them in the media uploader.

How can I retrieve the custom URL assigned to each image into $caption_text= at the bottom?

    $images =& get_children( array( 
                            'post_parent' => $featured_page,
                            'post_status' => 'inherit',
                            'post_type' => 'attachment',
                            'post_mime_type' => 'image',
                            'order' => 'ASC',
                            'numberposts' => $thelimit,
                            'orderby' => 'menu_order' )

// Loop through the images
foreach ( $images as $id => $image ) {
    $attatchmentID = $image->ID;

    $imagearray = wp_get_attachment_image_src( $attatchmentID , 'fullsize', false);
    $imageURI = $imagearray[0];
    $imageID = get_post($attatchmentID);

    $thumb_imagearray = wp_get_attachment_image_src( $attatchmentID , 'fullscreen-thumbnails', false);
    $thumb_imageURI = $thumb_imagearray[0];

    $imageTitle = $image->post_title;
    $imageDesc = $image->post_content;
    $postlink = get_permalink($image->post_parent);
    // If linking is On
    if ($featured_linked == 1 || $featured_linked == true) {
        $attatchmentURL = get_attachment_link($image->ID);
    // Count
    if ($count>1) { echo ","; }


    //Find and replace all new lines to BR tags
    $find   = array("\r\n", "\n", "\r");
    $replace = '<br />';
    $imageDesc = str_replace($find, $replace , $imageDesc);
    $image_post_link = get_permalink();

    if ($imageTitle) $slideshow_title='<div class="slideshow_title">'. esc_attr($imageTitle) .'</div>';
    if ($imageDesc) $slideshow_caption='<div class="slideshow_caption">'. $imageDesc .'</div>';

    $caption_text= '<div class="slideshow_captionwrap">' . $slideshow_title . $slideshow_caption . '> <a class="slideshow_cap_link" href="' . $image_post_link . '">Preview Article</a></div>';

    echo "{image : '".$imageURI."', title : '". $caption_text . "', thumb : '".$thumb_imageURI."', url : ''}";
  • Which version of WordPress are you using? Cause I am checking it in version 3.6 and I don't see the custom url field? – Maruti Mohanty Nov 6 '13 at 17:48
  • 1
    Ooops, my mistake, I didn't realize that the custom url field is only available when inserting the image to the post. In that case, is there a way to add an extra field to each image that can store a custom URL that I can call? – gteh Nov 6 '13 at 18:00
  • Try Advanced Custom Fields, when you create a field group with it, you can choose the Attachments as a rule. – passatgt Nov 6 '13 at 19:23
  • Thanks. good idea. Found a similar solution to add fields to attachments. – gteh Nov 6 '13 at 19:46

I found the solution here: http://wp.tutsplus.com/tutorials/creative-coding/how-to-add-custom-fields-to-attachments/

It's a simple plugin that adds custom fields to the attachment media editor. You can modify the fields and then display the values within your theme.

Thanks all for the suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.