Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline HTML background-image using PHP logic?

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.

like image 664
Lauren F Avatar asked Apr 07 '26 01:04

Lauren F


1 Answers

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;}
like image 159
Moishy Avatar answered Apr 09 '26 15:04

Moishy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!