Get wp_get_attachment_url outside of loop

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.

Read More

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!

Related posts

5 comments

  1. 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;
    
  2. 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 🙂

  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 );
    }
    
  4. 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.

  5. 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;
    

Comments are closed.