WordPress.org

Ready to get started?Download WordPress

Forums

Adding caption to image link. (4 posts)

  1. blackbookdesign
    Member
    Posted 5 years ago #

    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

  2. blackbookdesign
    Member
    Posted 5 years ago #

    bumbping :)

  3. Ionut
    Member
    Posted 4 years ago #

    hi, have you found a way how to do this?

  4. TheDeadMedic
    Member
    Posted 4 years ago #

    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');

Topic Closed

This topic has been closed to new replies.

About this Topic