Add Class to Div Based on Orientation of Featured Image

Style images differently according to weather the image is landscape, portrait, or even square.

<?php if ( has_post_thumbnail()) { 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
$image_w = $image[1];
$image_h = $image[2]; ?>

<div class="<?php echo $base_class; ?>__post-thumbnail
<?php if ($image_w > $image_h) {
echo 'landscape';
} elseif ($image_w == $image_h) {
echo 'square';
} else {
echo 'portrait';
} ?>">
<?php the_post_thumbnail(); ?></a>
<?php } ?>

The above will add a class for each orientation that can be targeted using CSS.


Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.