Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Please explain what your question is :) –  kaiser Nov 3 '13 at 21:21
    
I got it fixed. I needed the image url, but had to get it outside the loop. –  Alexnl Nov 4 '13 at 1:02
add comment

3 Answers 3

up vote 1 down vote accepted

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;
share|improve this answer
    
Using this I got an output of "Array" in plain text instead of the attachment url. I used 'full' as the optional size. –  Alexnl Nov 4 '13 at 0:32
    
Sorry, that's true, wp_get_attachment_image_src returns an array. I have edited the answer now. –  windyjonas Nov 4 '13 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 Nov 4 '13 at 19:25
    
I chose to use this one because it gave me a fallback should a featured image not be specified –  Alexnl Nov 5 '13 at 22:16
add comment

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.

share|improve this answer
add comment

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 :)

share|improve this answer
    
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 Nov 4 '13 at 0:40
    
@Alexnl: Fixed. –  s_ha_dum Nov 4 '13 at 0:55
add comment

Your Answer

 
discard

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.