338 posts
  • Has been part of the Envato Community for over 4 years
  • Located in United Kingdom
  • Made it to the Authors' Hall of Fame
  • Sells items exclusively on Envato Market
+3 more
PaulWinslow says

Afternoon,

I’m trying to get the post thumbnail / featured image from a custom post-type to use it as the background image of each post in my loop..

I found this: http://wpengineer.com/2123/use-wordpress-post-thumbnail-as-background-image/

But it doesn’t work.

The fallback image is used just fine, but if you set a featured image to the post it doesn’t work, it doesn’t get the image URL at all, so I end up with..

.project-scroll { background: url(’ ‘) 50% 0 no-repeat fixed; }

At the moment I don’t see how the code in the tutorial is supposed to actually work, because I can’t see how it’s supposed to check if there’s an image set when my loop is in a custom page template elsewhere..

Has anybody managed to do this? Any help would be brilliant.

No inline styles.

Cheers,
Paul

1511 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says

Hi Paul,

here is an example function:

if ( ! function_exists( 'prefix_post_background' ) ) {

    function prefix_post_background( $post_id = null, $echo = true ) {

        if ( ! $post_id ) $post_id = get_the_ID(); // id not passed, get current post in a loop

        if ( ! $post_id ) return; // could not get post id, and it was not passed in a function

        $thumb_id = get_post_thumbnail_id( $post_id );

        if ( ! $thumb_id ) return; // no featured image set

        $img_src = wp_get_attachment_image_src( $thumb_id, 'full', true );

        $custom_css = 
            "<style type='text/css'>
                #post-$post_id {
                    background-image: url( '$img_src[0]' );
                }
            </style>";

        if ( ! $echo ) return $custom_css;

        echo $custom_css;

    }

}

You would place it in your custom loop code (function call I mean).

338 posts
  • Has been part of the Envato Community for over 4 years
  • Located in United Kingdom
  • Made it to the Authors' Hall of Fame
  • Sells items exclusively on Envato Market
+3 more
PaulWinslow says

Am I supposed to replace some of these variables with something else? $post_id etc?

338 posts
  • Has been part of the Envato Community for over 4 years
  • Located in United Kingdom
  • Made it to the Authors' Hall of Fame
  • Sells items exclusively on Envato Market
+3 more
PaulWinslow says

Ahhh that’s perfect. I could kiss you!

1511 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says

You are welcome :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by