I was looking for a code snippet that will extract the URL of a post thumbnail to be used as a background image. Through research elsewhere, the best answer I found is the code snippet below:
<div style="background: url(<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,'large', true); echo $image_url[0]; ?>)"></div>
This code sets the large thumbnail of a post as the background image in this div tag. From here I believe you should be able to modify this code to your particular situation.
Related Posts:
- Cannot get full thumbnail size using the_post_thumbnail
- Full width thumbnail
- WP “optimizing” PNGs into thumbnails 5X larger than originals – FIX
- optimized PNGs 5X larger than originals
- The the_post_thumbnail without srcset?
- Get the first image from post content (eg.: hotlinked images)
- How to add a 2nd “featured image” to a post?
- How to change post thumbnail title and alt attributes to post title?
- Unexpected width and srcset attributes for the_post_thumbnail();
- How to use SRCSET with get_the_post_thumbnail()?
- How to handle thumbnails
- Change display of featured image for pages in twenty seventeen theme
- How to set dimensions of the post thumbnails (featured images)
- How to regenerate thumbnails when they’re stored on S3
- How to make thumbnail image fit into a div where image dimentions are completely different?
- Can’t show custom post thumbnail sizes as background images
- Featured Images are not able to be set
- Display recent posts with thumbnail
- Reasons why all thumbnail sizes aren’t working
- How do I Add images uploaded in the post to a default custom field
- How do I build a post_thumbnail based archive? [closed]
- Reactor Theme: Prevent Post Thumb on Post
- Featured Image meta box not showing up
- ‘Post-thumbnails’ feature does not seem to register
- How do I remove inline style in featured image markup?
- Theme thumbnail in dashboard
- Set a featured image as a replacement to the header image
- Dynamic Post Thumbnail →
- Issue on Getting Custom post type Thumbnail’s URL
- How To Pick Custom Size Of Uploaded Image In Theme Via the_post_thumbnail();?
- Admin – no Featured image choice in create new/page|post
- How to disable thumbnail filter for a specific template part or image size?
- the_post_thumbnail() get the URL to the “medium” sized image
- Full width featured thumbnail
- How can I get the featured image or the first image for a post/page and display it as a banner?
- How to make excerpt image be full size instead of thumbnail?
- How can I scale down the width of images in bulk that are embedded in posts throughout the site?
- Add image URL automatically to custom field?
- theme directory fallback image outputs url and not the image
- Created blank theme for REST API, featured image not appearing on admin side
- Featured Image Module Randomly Disappears
- How to correctly add the featured immage (thumbnails) support in a WP theme? Why I can’t add a featured immage?
- Single Featured Image Repeating
- Any disadvandages if I style the wordpress align classes without img and .wp-caption prefix
- apply_filters to featured image
- Show post thumbnail only if it exists using timthumb
- Display Page featured Image as well as Posts featured Image
- How do I work with responsive images in WordPress? (img srcset/sizes)
- I want to resize post featured image without cropping image. How?
- How to avoid Ajax in wordpress theme in making mulitple request?
- Custom CSS no getting applied
- Two theme options pages for a theme
- Import from HubSpot COS into WordPress 4?
- Trouble Retrieving Thumbnail URL on Front-Page
- Theme Development for a Newbie
- child comments not displaying as nested
- How can I fix the even/odd child classes comments so that the comments are unaffected by the top-level even/odd?
- How do I get my theme scripts to load in a custom theme built from Bootstrap
- How to add custom field option with menu?
- Adding Metabox Value Using the content filter
- wp_update_comment not working
- save prevision data in the database on theme activation
- Detect error 404 page to find out the issue
- Incorrect search results in search.php – issue triggered by using `define(‘BodyID’, ‘search’); the_post();` in search.php
- WordPress 3.9 two menus in same position?
- WordPress Footer Widget Processing
- Image load issue with custom page template on Internet Explorer
- using theme check plugin to remove waring and errors from my theme
- Flexslider not working for my custom theme WordPress
- paragraph format in WYSIWYG on a custom theme?
- Custom widget not appearing on front end
- unable to display the image meta value as background
- editor style css and page template with and without sidebar
- Conditional loop based on current page
- Dynamic meta description
- 3 x 3 grid of posts on the home page
- Theme Development : License help
- show posts in different styling
- Horizontal Navigation
- wpdb->insert adds too many rows
- get the post thumbnail returns the full size
- Show category and description
- Trouble creating custom sanitization function when uploading video files
- How to store and retrieve the attachment alignment?
- How can I demonstrate themes well?
- TinyMCE strips content while pasting. Possibly a bug
- Support Multiple featured images
- Enqueue Stylesheets After Theme’s “rtl.css”
- What’s the policy for building a theme that doesn’t support widgets/menus?
- Change default options on attachment page
- “No Data Received” error in Chrome every time i try to update the theme functions file
- WordPress 3.5 + Foundation 5 not working on IE9
- FitVid not working for iframes on particular theme install
- Thumbnail of the latest posts page
- problem with blank page
- Creating a Playlist of Widgets
- Loop through all posts, show attachment if there
- How to display only some widgets of a sidebar?
- How To Add Memory?
- How Does One Create a Global Variable Repository