I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.

if ( $post->post_parent )
    $post_id = $post->post_parent;
    $post_id = $post->ID;

if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
    echo $thumbnail;
  • 1
    You can use get_the_post_thumbnail_url() for get image url and put into <div class="bg-img" style="background:PHP VARIABLE HERE"> – Pratik Patel Nov 15 '18 at 6:24

please use this code

<?php $backgroundImg = wp_get_attachment_image_src( 
   get_post_thumbnail_id($post->ID), 'full' );?>

 <div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat;">

 <header class="entry-header">

 <h1 class="entry-title">
 <?php the_title(); ?>


  • How can you make the image appear for the parent and automatically apply to children like the code I pasted? – William Jerome Nov 16 '18 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.