Title: Adding caption to image link.
Last modified: August 19, 2016

---

# Adding caption to image link.

 *  [blackbookdesign](https://wordpress.org/support/users/blackbookdesign/)
 * (@blackbookdesign)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-caption-to-image-link/)
 * Hi I am trrying to integrate the image caption in the image href.
 * It’s getting a bit complicated for my skills!
 * here’s the code i need to modify in media.php:
 *     ```
       <div class='gallery'>");
   
       	$i = 0;
       	foreach ( $attachments as $id => $attachment ) {
       		$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
   
       		$output .= "<{$itemtag} class='gallery-item'>";
       		$output .= "
       			<{$icontag} class='gallery-icon'>
       				$link
       			";
       		if ( $captiontag && trim($attachment->post_excerpt) ) {
       			$output .= "
       				<{$captiontag} class='gallery-caption'>
       				{$attachment->post_excerpt}
       				</{$captiontag}>";
       		}
       		$output .= "</{$icontag}></{$itemtag}>";
       		if ( $columns > 0 && ++$i % $columns == 0 )
       			$output .= '<br style="clear: both" />';
       	}
       ```
   
 * I know its in this part:`$link = isset($attr['link']) && 'file' == $attr['link']?
   wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id,
   $size, true, false);`
    but I am not able to put my caption inthe $link…
 * If somebody knows that would be great.
    Thanks heaps.
 * (if you’re wondering why I want this follow this link:[css hover description](http://www.sohtanaka.com/web-design/css-on-hover-image-captions/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [blackbookdesign](https://wordpress.org/support/users/blackbookdesign/)
 * (@blackbookdesign)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/adding-caption-to-image-link/#post-944172)
 * bumbping 🙂
 *  [Ionut](https://wordpress.org/support/users/ionut/)
 * (@ionut)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/adding-caption-to-image-link/#post-944361)
 * hi, have you found a way how to do this?
 *  [TheDeadMedic](https://wordpress.org/support/users/thedeadmedic/)
 * (@thedeadmedic)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/adding-caption-to-image-link/#post-944362)
 * Firstly, I would recommend making these modifications in a plugin, or more easily,
   in your theme’s `functions.php`.
 * From the link you provided, I’ve taken a guess at what you are after. Copy the
   below into your theme’s `functions.php` and see how it goes!
 * Note: I have removed the `<style>..</style>` tags from the gallery output. You
   should put the CSS you require in your theme’s `style.css`.
 *     ```
       function tt_gallery_shortcode($attr)
       {
       	global $post;
   
       	static $instance = 0;
       	$instance++;
   
       	// Allow plugins/themes to override the default gallery template.
       	$output = apply_filters('post_gallery', '', $attr);
       	if ( $output != '' )
       		return $output;
   
       	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
       	if ( isset( $attr['orderby'] ) ) {
       		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
       		if ( !$attr['orderby'] )
       			unset( $attr['orderby'] );
       	}
   
       	extract(shortcode_atts(array(
       		'order'      => 'ASC',
       		'orderby'    => 'menu_order ID',
       		'id'         => $post->ID,
       		'itemtag'    => 'div',
       		'icontag'    => 'span',
       		'captiontag' => 'span',
       		'columns'    => 3,
       		'size'       => 'thumbnail'
       	), $attr));
   
       	$id = intval($id);
       	$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
   
       	if ( empty($attachments) )
       		return '';
   
       	if ( is_feed() ) {
       		$output = "\n";
       		foreach ( $attachments as $att_id => $attachment )
       			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
       		return $output;
       	}
   
       	$itemtag = tag_escape($itemtag);
       	$captiontag = tag_escape($captiontag);
       	$columns = intval($columns);
       	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
   
       	$selector = "gallery-{$instance}";
   
       	$output = apply_filters('gallery_style', "
       		<div id='$selector' class='gallery galleryid-{$id}'>");
   
       	$i = 0;
       	foreach ( $attachments as $id => $attachment ) {
   
       		$link = wp_get_attachment_link($id, $size, true, false);
   
       		$link_caption = "<{$captiontag} class='desc'>" . wptexturize($attachment->post_excerpt) . "</{$captiontag}>";
   
       		// splice the caption inside the anchor tag
       		$link = preg_replace('|</a>|', $link_caption . '</a>', $link);
   
       		$output .= "<{$itemtag} class='gallery-item imgteaser'>
       		";
       		$output .= $link;
       		$output .= "</{$itemtag}>
       		";
   
       		if ( $columns > 0 && ++$i % $columns == 0 )
       			$output .= '<br style="clear: both" />';
       	}
   
       	$output .= "
       			<br style='clear: both;' />
       		</div>\n";
   
       	return $output;
       }
   
       add_shortcode('gallery', 'tt_gallery_shortcode');
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Adding caption to image link.’ is closed to new replies.

## Tags

 * [caption](https://wordpress.org/support/topic-tag/caption/)
 * [code](https://wordpress.org/support/topic-tag/code/)
 * [images](https://wordpress.org/support/topic-tag/images/)
 * [link](https://wordpress.org/support/topic-tag/link/)
 * [media](https://wordpress.org/support/topic-tag/media/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 3 replies
 * 3 participants
 * Last reply from: [TheDeadMedic](https://wordpress.org/support/users/thedeadmedic/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/adding-caption-to-image-link/#post-944362)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
