Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Retrieving a custom link on an attachment

I am using images on a Wordpress page to link to other pages in the site. I know how to get an attached image's file or attachment URL, but I can't figure out how to get its URL if it's linked to another page.

Take a look at here .The first image, "Women's clothes" links to a separate page in the site. I'm trying to make the caption of the attachment have the same link.

I know how I can use the filter below to alter the caption to wrap it in an anchor tag, but I don't know how actually retrieve the URL of the attachment if it links to a separate page. Anyone have any ideas?

//Change Caption output
function my_caption($output, $attr , $content) {
    extract(shortcode_atts(array(
        'id'    => '',
        'align' => 'alignnone',
        'width' => '',
        'caption' => ''
    ), $attr));

    if ( 1 > (int) $width || empty($caption) )
        return $content;

    if ( $id ) $id = 'id="' . esc_attr($id) . '" ';

    return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
    . do_shortcode( $content ) . '<a href="#"><p class="wp-caption-text">' . $caption . '</p></a></div>';return $output;
}
add_filter('img_caption_shortcode','my_caption' , 10 , 3 );
share|improve this question

marked as duplicate by toscho Dec 15 '12 at 23:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
the url you insert when you add an attachment to a post is saved with the post content, not the attachment. –  Milo Jul 12 '11 at 16:42
    
Thank you for pointing that out. I should have just looked at the HTML, but for some reason I was convinced it was part of the attachment –  user716816 Jul 14 '11 at 18:43
    
which code you're using to show the image ? –  amit Aug 4 '12 at 21:43
    
Solution on this page wordpress.stackexchange.com/a/14776 I've tried it and it works:) –  webwhale Oct 19 '12 at 4:30

1 Answer 1

You could do it with jQuery fairly simply.

$("#caption").src($("img").src);

is psuedo code to get you thinking down the right track. Basically, loop through the images, grab the source, and apply it to the element that you want to gain the link.

share|improve this answer

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