Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter doo0

    (@doo0)

    Yup, image will be the same, just smaller and cropped different.

    Media queries and responsive grid also changes things to be better seen on different devices so why changing aspect ratio of image is a bad thing?

    I can’t use ‘object-fit: cover’ because of lack support from MS browser (polyfill isn’t working as well).

    I can’t use ‘background: cover’ because it’s for featured image.

    I can’t or maybe won’t use css crop (that thingy with overflow: hidden) because it will load unnecessary parts of image (in some case 30-40% more).

    So if srcset isn’t good approach – what is?

    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 2 replies - 1 through 2 (of 2 total)