• Resolved doo0

    (@doo0)


    So, I want to add caption to featured image in my posts. It’s not a thumbnail, I’m using roots theme.

    I’ve modified init.php like this…

    add_theme_support('post-thumbnails');
    set_post_thumbnail_size(150, 150, true);
    add_image_size('category-thumb', 750, 300, true);

    …and for displaying it I’m using this one:

    <?php the_post_thumbnail('category-thumb', array('class' => 'aligncenter'))?>

    I’ve followed those topics around the web, but I can only show thumbnail and not my 'category-thumb'.

Viewing 1 replies (of 1 total)
  • Thread Starter doo0

    (@doo0)

    Ok, I’ve resolved it.

    Here is solution: Second-cup-of-coffee

    I’ve modified it a bit to get caption below image:

    function featured_image_in_post( ) {
    global $post;
    $thumbnail_id = get_post_thumbnail_id($post->ID);
    $thumbnail_details = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
    $thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, 'category-thumb' );
    $thumbnail_width = $thumbnail_src[1];
    
    if ($thumbnail_src && isset($thumbnail_src[0])) {
    echo '<div class="featured-image';
    if ( !empty( $thumbnail_details[0]->post_excerpt ) ) echo ' wp-caption';
    echo '" style="max-width: ' . $thumbnail_width . 'px;">';
    the_post_thumbnail( $post->ID );
    if ( !empty( $thumbnail_details[0]->post_excerpt ) ) {
    echo '<p class="featured-image-caption">';
    echo $thumbnail_details[0]->post_excerpt;
    echo '</p>';
    }
    echo '</div>';
    }
    }

    and input:

    <?php if ( has_post_thumbnail() ) :
    featured_image_in_post();
    endif; ?>

    All kudos goes to link above.

Viewing 1 replies (of 1 total)

The topic ‘Featured image in post with caption’ is closed to new replies.