Is it possible to use my PHP logic used to display a WordPress post’s featured image as the inline background image of a div? Here is my attempt:
<div class="rounding-image" style="background-image: url(
<?php
if ( $thumbnail == true ) {
// If the post has a feature image, show it
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// Else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
)"></div>
This isn’t throwing a specific error, but rather displaying )">
where the image would be.
the_post_thumbnail displays the image with the
img
tag. What you want to do is get the image URLI like to use background-size: cover if its full width. if it’s not just remove that part of the css
Use this, it will set the background image to the featured image if one exists. This allows you to set a default via CSS in case a featured image is not set.