最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Show thumbnail depending on image width

programmeradmin6浏览0评论

I'm trying to display the images in Landscape or Portrait mode depending on the width of the image I upload to the "featured image" of each WordPress article.

If the width of the image is greater than the height, it stays with the default thumb, but if the width is less, select the "image-vertical-inside-single" thumb.

I have an add_image_size( 'image-vertical-inside-single', 450, 99999, false ); So I can create the image that size.

And the following code...

 <?php
        $thumb_single            = 'imagen-vertical-inside-single';
        $imgData     = wp_get_attachment_metadata( get_post_thumbnail_id( get_the_ID() ) );
        $width       = $imgData['width'];
        $height      = $imgData['height'];

        if ( $width > $height || $width == $height ) {
            $thumb_single = '';
        } else {
            if ( '' != get_the_title() ) {
                $thumb_single = 'imagen-vertical-inside-single';
            }
        }

        return $thumb_single;

?>

            <figure class="featured-image">
                <?php the_post_thumbnail( $thumb_single, array('class' => 'skip-lazy') ); ?>
            </figure>

But it doesn't work properly...

I'm trying to display the images in Landscape or Portrait mode depending on the width of the image I upload to the "featured image" of each WordPress article.

If the width of the image is greater than the height, it stays with the default thumb, but if the width is less, select the "image-vertical-inside-single" thumb.

I have an add_image_size( 'image-vertical-inside-single', 450, 99999, false ); So I can create the image that size.

And the following code...

 <?php
        $thumb_single            = 'imagen-vertical-inside-single';
        $imgData     = wp_get_attachment_metadata( get_post_thumbnail_id( get_the_ID() ) );
        $width       = $imgData['width'];
        $height      = $imgData['height'];

        if ( $width > $height || $width == $height ) {
            $thumb_single = '';
        } else {
            if ( '' != get_the_title() ) {
                $thumb_single = 'imagen-vertical-inside-single';
            }
        }

        return $thumb_single;

?>

            <figure class="featured-image">
                <?php the_post_thumbnail( $thumb_single, array('class' => 'skip-lazy') ); ?>
            </figure>

But it doesn't work properly...

Share Improve this question asked Jan 24, 2020 at 14:04 MikeMike 1
Add a comment  | 

1 Answer 1

Reset to default 0

I think the problem is that you're trying to get the metadata for an image without specifying which size you want the width and height from.

    $imgData     = wp_get_attachment_metadata( get_post_thumbnail_id( get_the_ID() ) );
    $width       = $imgData['width'];
    $height      = $imgData['height'];

Instead try this:

    $imgID       = get_post_thumbnail_id( get_the_ID() );
    $imgURL      = wp_get_attachment_image_src( $imgID, 'full' ); // now I know which image specifically I want - in this case 'full'.
    $width       = $imgURL['1']; // I've never tested 'height' and 'width', but 100% the array numbers work
    $height      = $imgURL['2'];

Now I know the height and width of the specific img url I'm testing.

发布评论

评论列表(0)

  1. 暂无评论