Hi, my name is
Lee Harris and
I'm a Freelance Web Designer

WordPress: Get width and height of the featured image

24.May.2022 | ,

Get the featured image dimensions (width and height) in WordPress. Paste the following code in the single.php file.

<?php

$post_id = get_the_ID();
$image_data = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), "thumbnail" );
$image_width = $image_data[1];
$image_height = $image_data[2];

?>

If you want to get a specific featured image and place it anywhere other than single.php, change the $post_id variable to the post ID you want to get the featured image from.

  • html5
  • css3
  • sass
  • php
  • WordPress
  • WooCommerce