WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) {
        '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 was marked as an exact duplicate of an existing 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

You could do it with jQuery fairly simply.


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.