4

I need to get the featured image outside the loop. This is so that I can have a different full-screen background image for each page, set by the featured image.

After doing some research I was able to get the post ID outside the loop.

This is what I've got:

$page_object = get_queried_object();
$page_id     = get_queried_object_id();
$bkgdImg = wp_get_attachment_url( $page_id );
if (!empty($bkgdImg)) {
    $backgroundImg = $bkgdImg;
}
else {
    $defaultbackground = . get_template_directory_uri() . "/images/default-background.jpg";
    $backgroundImg = $defaultBackground;
}

echo $backgroundImg;

Thanks!

2
  • Please explain what your question is :)
    – kaiser
    Commented Nov 3, 2013 at 21:21
  • I got it fixed. I needed the image url, but had to get it outside the loop.
    – Alexnl
    Commented Nov 4, 2013 at 1:02

5 Answers 5

8

if the result you're looking for is a printout of the URL, like in your example, then this should work:

$page_id = get_queried_object_id();
if ( has_post_thumbnail( $page_id ) ) :
    $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page_id ), 'optional-size' );
    $image = $image_array[0];
else :
    $image = get_template_directory_uri() . '/images/default-background.jpg';
endif;
echo $image;
5
  • Using this I got an output of "Array" in plain text instead of the attachment url. I used 'full' as the optional size.
    – Alexnl
    Commented Nov 4, 2013 at 0:32
  • Sorry, that's true, wp_get_attachment_image_src returns an array. I have edited the answer now.
    – windyjonas
    Commented Nov 4, 2013 at 7:09
  • Thanks!! it worked. Can you tell me from coding perspective which solution would be better? I know in your case it has a fallback should a featured image not be specified.
    – Alexnl
    Commented Nov 4, 2013 at 19:25
  • I chose to use this one because it gave me a fallback should a featured image not be specified
    – Alexnl
    Commented Nov 5, 2013 at 22:16
  • Using this in a WP_Query I had to change get_queried_object_id() to get_the_id
    – Pixelsmith
    Commented Jul 18, 2019 at 22:25
3

You have a couple of syntax errors, but in your question you state that...

This is so that I can have a different full-screen background image for each page, set by the featured image.

wp_get_attachment_url takes the actual attachment ID, not the page ID to which the featured image is attached. I therefore assume that the question is "Why am I not getting the image URL that I expect?"

What you need is to use get_post_thumbnail_id and pass that to wp_get_attacment_url...

$bkgdImg = wp_get_attachment_url(get_post_thumbnail_id( $page_id ));

... possibly with a second argument to specify size.

I hope I guessed correctly :)

2
  • I am trying to get the image url. Trying this got me the image when I echoed the $bkgdImg along with all the formatting wordpress has with it. I'd like to get just the URL. Thanks!
    – Alexnl
    Commented Nov 4, 2013 at 0:40
  • @Alexnl: Fixed.
    – s_ha_dum
    Commented Nov 4, 2013 at 0:55
3

Little late here, but I created a solution that is a bit cleaner and reusable with some of the suggestions in this thread. Add this anywhere in your functions.php file, then simply call this function wherever you need it. You can supply the post id if you have it, if not it's not required.

function iprice_get_attachment_url($post_id = null) {
    $post = $post_id ? get_post( $post_id ) : get_post( get_the_ID() );
    $thumbnail_id = get_post_thumbnail_id($post);
    $thumbnail_url = wp_get_attachment_url($thumbnail_id);
    return apply_filters( 'wp_get_attachment_url', $thumbnail_url, $post );
}
2

This got it working and got me the image URL:

$page_id = get_queried_object_id();
$post_thumbnail_id = get_post_thumbnail_id( $page_id );
$bkgdImg = wp_get_attachment_url( $post_thumbnail_id );
echo $bkgdImg;

Thanks for all your help, it put me in the right direction.

2

Add theme support for thumbnail add_theme_support('post-thumbnails');

Then write the below code on your archive page. It works before your WordPress loop

$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
echo $thumb_url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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