WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieving featured image URL (12 posts)

  1. dmatthams
    Member
    Posted 4 years ago #

    Does anyone know of a way to retrieve only the URL of the featured image for a post? I can get the image to display wrapped in an <img> tag by using

    <?php the_post_thumbnail( 'single-post-thumbnail' ); ?>

    But i want to use the featured image as a background to a div, so this isn't helpful.

    Any help greatly appreciated.

  2. Digital Raindrops
    Member
    Posted 4 years ago #

    Something like setting the div background as a style adapted from the twenty ten code, and untested code!

    See if there is an image first and display the div if there is

    <?php if (has_post_thumbnail( 'single-post-thumbnail' )) : ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <div id="custom-bg" style="background-image: url('<?php echo $image; ?>')">
    
    </div>
    <?php endif; ?>

    David

  3. dmatthams
    Member
    Posted 4 years ago #

    Thanks for the reply, but i couldn't get this to work, the IF statement returned false even if there was a featured image to a post. Further googling led me to some code which returns the url and works beautifully:

    <div class="feat" style="background-image: url('<?php
    $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
    	echo wp_get_attachment_url( $attachment_id, 'medium' );
    } ?>')">
  4. Digital Raindrops
    Member
    Posted 4 years ago #

    I had a quick look and I added the wrong argument with the has_post_thumbnail, it needs the post id or nothing has_post_thumbnail( $post_id ) or has_post_thumbnail( ).

    The corrected code just in case!

    <?php if (has_post_thumbnail( $post->ID ) : ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <div id="custom-bg" style="background-image: url('<?php echo $image; ?>')">
    
    </div>
    <?php endif; ?>

    David :)

  5. styledev
    Member
    Posted 4 years ago #

    Fixed code. It was missing a ')' in the IF and $image is an array so made the call to $image[0] for the src url.

    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
    
    </div>
    <?php endif; ?>
  6. Li-An
    Member
    Posted 3 years ago #

    Thank you a lot, Styledev, I was searching this for monthes !

  7. BeingMaverick
    Member
    Posted 3 years ago #

    Brilliant.. solved my problem.. Thanks a alot styledev..

  8. billzy
    Member
    Posted 3 years ago #

    Great work... exactly what I was looking for, thank you very much.

  9. lucasrcosta
    Member
    Posted 3 years ago #

    Thanks, great code. Fits like a glove...

  10. spoila
    Member
    Posted 3 years ago #

    Should this code work with a page as well as a post? It doesnt display anything for my pages.

  11. wwalker
    Member
    Posted 3 years ago #

    styledev, the code works.
    However if there isn't a featured image, how do I handle the "else" condition?

    My theme has a bg image for the div. I want it to be the featured image (if one is available) and the default image if one is not available.

  12. funkyole
    Member
    Posted 3 years ago #

    Good stuff, also works on wp 3.0 :)

    cheers

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags