WordPress.org

Ready to get started?Download WordPress

Forums

Get the Image
Does get_the_image support description/caption? (5 posts)

  1. Mark
    Member
    Posted 1 year ago #

    Get the image is clearly one of the nicest plugins around for image
    handling in WordPress. Beautifully executed with sensible defaults.

    However, how can we get at the image description or caption using get_the_image()? If this is not possible, is this functionality perhaps planned?

    Here are some reasons to have descriptions or captions:

    1. Accessibility. Images should have alt texts, and if we can't even set the alt text using get the image that's a bit of a bummer.
    2. Licensing. Images sometimes require attribution. We can use the WordPress description or caption fields for this info, but that is of little use if we can't get at the info with the nicest plugin around.
    3. Usability. An image says more than a 1000 words, but a few words can radically alter our interpretation of an image. Captions and descriptions are often crucial parts of the image, and it would be great if get_the_image() supported them.

    I'm hoping future versions of the plugin will enable us to get_the_caption too!

    http://wordpress.org/extend/plugins/get-the-image/

  2. Mark
    Member
    Posted 1 year ago #

    Okay here's the answer. I overlooked that the alt text was being set already due to my image being embedded in a link.

    For the benefit of others, this was to make the Flickr Pick a Picture plugin work with an Oxygen child theme. FPP puts the attribution, including a link to the Flickr photo page, in the image post_excerpt. Alt text can't include links so I was looking for a way to display the post_excerpt with the featured image in another unobtrusive way. I settled for a div that only appears when you hover over the image.

    First, I was able to throw together a function for getting the post_excerpt() field (what I was calling the caption). Here is the function that I added to my functions.php:

    function get_the_feature_caption() {
      global $post;
    
      $thumbnail_id    = get_post_thumbnail_id($post->ID);
      $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
    
      if ($thumbnail_image && isset($thumbnail_image[0])) {
        $caption = '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
      }
      return $caption;
    }

    Then in the post.php template of my Oxygen child theme, I include the following code:

    if ( current_theme_supports( 'get-the-image' ) )
    	$image= get_the_image( array( 'meta_key' => 'Thumbnail', 'size' => 'single-thumbnail', 'link_to_post' => false, 'image_class' => 'featured', 'attachment' => false, 'width' => 470, 'height' => 260, 'echo' => false ) );
    $caption = get_the_feature_caption();
    if ( !empty( $image ) ) {
    	echo '<div class="img-feature">' . $image ;
    	echo '<div class="feature-caption">' . $caption . '</div></div>';
    }

    Finally, in style.css of my child theme I use the following CSS to display the attribution in the lower right corner of the image — on hover only.

    .img-feature { position:relative; }
    .img-feature .feature-caption { display:none; }
    .img-feature:hover .feature-caption {
    	display:block;
    	position:absolute;
    	bottom:15px;
    	right:0px;
    	opacity:0.5;
    	background-color:#000;
    	padding:4px 8px;
    	color:#fff;
    	text-align:right;
    }
    .feature-caption a {
    	color:#fff;
    }
    .feature-caption a:hover {
    	color:#fff;
    }

    Hopefully this is helpful for others.

  3. simbelsim
    Member
    Posted 1 year ago #

    My website runs a child theme for Origin which has get_the_image built into the framework, and I've been quite frustrated that I can't get the captions to work, for licensing reasons as you've mentioned above.

    I followed your steps but, alas, it didn't help. As far as I can see, the discrepancy is in post.php. It's missing $image, and when I added it, your solution still didn't work. This is what my post.php featured image bit looks by default:

    <?php if ( current_theme_supports( 'get-the-image' ) ) get_the_image( array( 'meta_key' => 'Thumbnail', 'size' => 'single-thumbnail', 'link_to_post' => false, 'image_class' => 'featured', 'attachment' => false ) ); ?>

    I'm guessing there is something else I need to put into functions file to make this work. Any ideas?

    Currently the only function that applies to featured post image is

    function origin_image_sizes() {
    	add_image_size( 'single-thumbnail', 636, 310, true );}

    which fixes the size, and came by default with the theme.

    The website is http://pausetowonder.org, if that helps.

  4. simbelsim
    Member
    Posted 1 year ago #

    Never mind, I replaced the built-in get_the_image for post featured images with WordPress standard get_post_thumbnail function and then styled the captions accordingly. Much easier than bending the will of get_the_image to do something that the author clearly doesn't want it to.

  5. Justin Tadlock
    Member
    Plugin Author

    Posted 10 months ago #

    Caption support for the Get the Image plugin was just added.

    The version packaged with themes was updated about two months ago, so theme authors should be rolling out updates with the new features by now or very soon.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic