Viewing 9 replies - 1 through 9 (of 9 total)
  • Captions for post_thumbnails aren’t supported out of the box. You’d need to use a custom function for that. There are several such functions outlined in this other thread though.

    there is some code based on the linked forum topic: https://www.transformationpowertools.com/wordpress/wordpress-post-thumbnails-with-caption

    if you are not familiar with filter functions, read https://codex.www.ads-software.com/Plugin_API#Filters

    Thread Starter Dolomats0

    (@dolomats0)

    Thank you for your support

    This link :
    https://www.transformationpowertools.com/wordpress/wordpress-post-thumbnails-with-caption

    helped me insert the caption as u can see here

    https://ibergag.com/?p=9866

    but still cant get CSS to style it anyone can help ?!

    what exact code have you used?

    I don’t see any ‘thumbnail’ and ‘caption’ specific css classes with your thumbnail (assuming that this is the caption: PH.CAHFIK ARICH)

    Thread Starter Dolomats0

    (@dolomats0)

    i used this funcktion on the file function.php on my current théme folder

    //POST THUMBNAIL AND CAPTION STYLED SIMILAR TO .wp-caption//
    function the_post_thumbnail_and_caption($size = '', $attr = '') {
    global $post;
    
    $thumb_id = get_post_thumbnail_id($post->id);
    
    $args = array(
    'post_type' => 'attachment',
    'post_status' => null,
    'parent' => $post->ID,
    'include'  => $thumb_id
    );
    
    $thumbnail_image = get_posts($args);
    
    if ($thumb_id && $thumbnail_image && isset($thumbnail_image[0])) {
    
    $image = wp_get_attachment_image_src( $thumb_id, $size );
    $image_width = $image[1];
    
    $output = '<div style="width: ' . ($image_width) . 'px">';
    
    $attr['class'] = ''; //move any 'class' attributes to the outer div, and remove from the thumbnail
    
    $output .= get_the_post_thumbnail($post->ID, $size, $attr);
    
    /* //Uncomment to show thumbnail title
    $title = $thumbnail_image[0]->post_title;
    if($title) :
    $output .= '<p>';
    $output .= $title;
    $output .= '</p>';
    endif; */
    
    /* //Uncomment to show the thumbnail caption */
    $caption = $thumbnail_image[0]->post_excerpt;
    if($caption) :
    $output .= '<p>';
    $output .= $caption;
    $output .= '</p>';
    endif;
    
    /* //Uncomment to show the thumbnail description
    $descr = $thumbnail_image[0]->post_content;
    if($descr) :
    $output .= '<p>';
    $output .= $descr;
    $output .= '</p>';
    endif; */
    
    /* //Uncomment to show the thumbnail alt field
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    if(count($alt)) :
    $output .= '<p>';
    $output .= $alt;
    $output .= '</p>';
    endif; */
    
    $output .= '</div>';
    
    }
    echo $output;
    }

    and used this code on the file single.php on current théme folder

    <?php the_post_thumbnail_and_caption('large',array('class' => 'alignleft')); ?>

    and i tried this CSS code on the file style.css on my current théme folder but the CSS dindt work

    .thumbnail-caption { padding: 5px; background: #f5f5f5; border: 1px solid #ddd; }
    .thumbnail-caption-text { text-align: center; margin-bottom: 5px; font-size: 90%; }
    /*
    .thumbnail-title-text { text-align: center; margin-bottom: 5px; font-size: 90%; }
    .thumbnail-description-text { text-align: center; margin-bottom: 5px; font-size: 90%; }
    .thumbnail-alt-text { text-align: center; margin-bottom: 5px; font-size: 90%; }
    */
    Thread Starter Dolomats0

    (@dolomats0)

    yes PH.CAHFIK ARICH is the caption.
    (assuming that this is the caption: PH.CAHFIK ARICH)

    my mistake ;-(

    – the css classes were missing in the function code in the linked post;

    corrected (and redundant code removed):

    //POST THUMBNAIL AND CAPTION STYLED SIMILAR TO .wp-caption//
    function the_post_thumbnail_and_caption($size = '', $attr = '') {
    global $post;
    $thumb_id = get_post_thumbnail_id($post->id);
    	$args = array(
    		'post_type' => 'attachment',
    		'post_status' => null,
    		'parent' => $post->ID,
    		'include'  => $thumb_id
    	);
    
    $thumbnail_image = get_posts($args);
    
    if ($thumb_id && $thumbnail_image && isset($thumbnail_image[0])) {
    	$image = wp_get_attachment_image_src( $thumb_id, $size );
    	$image_width = $image[1];
    
    	if($attr) $attr_class = $attr['class'];
    	$attr['class'] = ''; //move any 'class' attributes to the outer div, and remove from the thumbnail
    
    	$output = '<div class="thumbnail-caption attachment-'.$size.($attr?' '.$attr_class:'').'" style="width: ' . ($image_width) . 'px">';
    
    	$output .= get_the_post_thumbnail($post->ID, $size, $attr);
    
    	/* to show the thumbnail caption */
    	$caption = $thumbnail_image[0]->post_excerpt;
    	if($caption) {
    		$output .= '<p class="thumbnail-caption-text">';
    		$output .= $caption;
    		$output .= '</p>';
    	}
    
    	$output .= '</div>';
    	}
    echo $output;
    }
    Thread Starter Dolomats0

    (@dolomats0)

    Here we Go, it worked perfectly as u can see HERE

    it look soo mush better

    And big Thanks for your help

    this line made problems in wp3.5:
    $thumb_id = get_post_thumbnail_id($post->id);

    corrected to proper capitalisation:
    $thumb_id = get_post_thumbnail_id($post->ID);

    full code for wp3.5:
    https://pastebin.com/SkGyX1St

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Thumbnails Caption’ is closed to new replies.