• Hi,

    I’m trying to figure out on how to get image caption to show on my featured images in my articles.

    I am pretty new to WordPress so forgive me if this is something simple to most of you. If so, better for me because it means my problem can be quickly taken cared of.

    Anyways I want my featured images to show a small caption (which I will use as an image source) on the bottom left corner of the picture, inside a black rectangle.

    Here is an example of what I mean

    Can anyone possibly help me with this? I’ve been trying to do this for months and either screw something up or can’t get anything to work… I have also looked around a lot on the web and could not find anyone trying to do the same as me.

    Much thanks

Viewing 1 replies (of 1 total)
  • have a look at http://www.transformationpowertools.com/wordpress/wordpress-post-thumbnails-with-caption – the second part after the PS;

    code example for a filter acting on 'post_thumbnail_html' (to be added into functions.php of your theme):

    // 27/01/2012 alchymyth
    // show thumbnail with caption, if available, wrapped with '.wp-caption thumb-caption' div;
    // show just the thumbnail otherwise
    add_filter('post_thumbnail_html','add_post_thumbnail_caption',10,5);
    function add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {
    
    if( $html == '' ) {
    
      return $html;
    
    } else {
    
      $out = '';
    
    $thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
    
    if ($thumbnail_image && isset($thumbnail_image[0])) {
    
      $image = wp_get_attachment_image_src($post_thumbnail_id, $size);
    $t_width = $image[1] +0; // +0 here for no extra padding, needs to be considered in writing css - the default image caption uses +10;
      $class = $attr['class'];
      if($thumbnail_image[0]->post_excerpt) $out .= '<div style="width:'.$t_width.'px; ">';
    
      $out .= $html;
    
      if($thumbnail_image[0]->post_excerpt) $out .= '<p>'.$thumbnail_image[0]->post_excerpt.'</p></div>';
    }
      return $out;
      }
    }

    (not tested with latest wp version)

Viewing 1 replies (of 1 total)
  • The topic ‘Image Caption/Source in Featured Image’ is closed to new replies.