Show thumbnail depending on image width

I'm trying to display the images in Landscape or Portrait mode depending on the width of the image I upload to the

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744806422a4594802.html

相关推荐

  • Show thumbnail depending on image width

    I'm trying to display the images in Landscape or Portrait mode depending on the width of the image I upload to the

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信