• Hi,

    I’m creating a ‘post overview’ page for a blog style website. On this page I want to display the featured image as a thumbnail but only when the post has one assigned to it. I’m loading the thumbnail as the background-image of a div because I need the width to be a % so that it adjust slightly to screen size. This is what I got so far:

    <?php
    if(has_post_thumbnail()) {
    global $post;
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 500,500 ), true, '' );
    echo ' <div class="image" style="background: url( <?php echo $src[0]; ?> ) !important;"></div> ';
    } else {
    }
    ?>

    I’m quite new to php so I’m not sure what i’m doing wrong. It seems that the <?php echo $src[0]; ?> ) !important; part doesn’t ‘translate’ into and valid html code that directs to the image.

Viewing 1 replies (of 1 total)
  • Thread Starter aafke

    (@aafke)

    problem resolved for anyone interested

    <?php
    
    if(has_post_thumbnail()) {
    global $post;
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 500,500 ), true, '' );
    ?>
    <div class="image" style="background: url( '<?php echo $src[0]; ?>' ) !important;"></div>
    <?php
    } else {
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Display featured image as background if has_post_thumbnail’ is closed to new replies.