WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm wondering if it's possible in the single.php template to use the width of the post's featured image elsewhere in the page.

What I'm trying to do is add a div element on the page with the same width as the post's featured image (which will always be a different width).

If anyone has any ideas, let me know.


share|improve this question
up vote 10 down vote accepted

Try the following. First, add this piece of code to the template:

<?php $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "thumbnail" ); ?>

$image_data will now return an array containing the url, width, and height (function reference). To get the width, you might do this:

<?php $image_width = $image_data[1]; ?>

In your specific example, after adding the two pieces of code above to your template, you might do this:

<div style="width:<?php echo $image_width; ?>">

Is that helpful?

share|improve this answer
That's incredibly helpful, worked like a charm. Thanks. – user5710 May 26 '11 at 17:18
You are most welcome! Glad you joined us on the WordPress Stack Exchange :). – Jonathan Wold May 26 '11 at 22:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.