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
- 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?
- When to use _e and __ for the translation?
- With WordPress themes, where do I store the images and files relatively?
- Should `get_template_directory_uri()` be escaped?
- Why doesn’t /2013/01/ properly return January’s archives in archive.php?
- two tinyMCE editors in the same page
- index.php is not loaded for single posts
- wp_head() not inserting the default stylesheet style.css
- Setting default thumbnails size & creating multiple thumbnail sizes
- WordPress Customizer Control with React
- how to implement wp_is_mobile on themes?
- How to disable automatic colors in the Twenty Twenty theme?
- Check if user has enrolled in Sensei LMS Course on Lesson Page
- Part of Menu Item Hidden on Header Menu
- Child theme style.css versioning
- Show Post Thumbnail In Custom Post From Other CPT
- Customize the previous_post_link output
- How to use get_template_part instead of include_once?
- How to remove comment link title attribute?
- Should we use ob_start() in WordPress short code
- Sidebar Thumbs Squished?
- Warning: A non-numeric value encountered in theme functions.php
- How can I ping update-core.php with a script?
- “Add A Widget” button in the Customizer
- loading blank white screen of slide
- Custom WordPress File Inclusion
- Redirect to another page using contact form 7? [closed]
- How to do a section of bulleted text within our page?
- What should I use? index.php, front-page.php and home.php
- How to change the theme directory uri for localhost?
- What is the diferences between pure WordPress theme and Woocommerce theme? [closed]
- Insert Rich Text Editor in theme?
- Customizer: get_preview_url() inside customize_save_after hook
- How to add custom meta box when you have a custom page template file
- Theme Check errors of theme code
- Does wordpress add their own classes into nav menus?
- Post archives link yields a 404 Not Found
- Getting white screen of death on category pages because of body_class() function
- Let user to upload multiple time
- Code showing in wordpress post titles around website
- register_theme_directory() sees custom themes directory, but blank frontend
- how to call options on front end and add logo
- Jetpack Infinite Scroll Not Working
- How to share posts (and plugins) between existing site and new, separate dev/test installation?
- Duplicate WordPress site to subdirectory but use same db?
- Menu order of parent menu from perspective of child menu
- Conditional loop based on current page
- Theme customizer live preview JS- Trying to bind to an html image url without luck
- wp_deregister_script was called incorrectly
- Add the ability of changing background color of a theme [closed]
- How to use esc_attr__() function properly to translate a variable that contains string?
- Purchased Theme to Custom Made Theme? [closed]
- How to make website with many template that active [closed]
- Query for tag given slug