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 URL
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); $url = $thumb['0'];
if ( has_post_thumbnail() ) {echo $url;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With