I have two custom post types in my theme. I want to display the featured image as background image. How can I do that

    <div class="owl-carousel">
            $args = array(
                'post_type' => 'post-slug'
            $query = new WP_Query($args);
             $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
        <?php if($query -> have_posts()): while($query -> have_posts()): $query -> the_post(); ?>
        <div class="item">
            <div class="container">
                <div class="row">    
                    <div class="col-sm-5 content-img" style="background-image: url('<?php echo $featured_img_url; ?>');">
                        <img src="<?php echo $featured_img_url; ?>" alt="" class="visible-sm visible-xs">
                    <div class="col-sm-7 col-sm-offset-5 content-detail">
                        <div class="team-content-wrapper wow fadeInRight" data-wow-duration="1500ms" data-wow-delay="1000ms">
                            <h1><?php the_title(); ?></h1>
                            <?php the_content(); ?>
                        </div> <!-- team content wrapper end -->
        <?php endwhile; endif; wp_reset_postdata(); ?>

Please help


You can use get_the_post_thumbnail($post, $size, $attrs)/the_post_thumbnail($post, $size, $attrs) for print image full taq or get_the_post_thumbnail_url($post, $size, $attrs) if you want return just featured image url.

Also you can use wp_get_attachment_image($attachment_id, $size, $atts) or wp_get_attachment_image_url($attachment_id, $size, $atts) for not featured images but from your media library.

| improve this answer | |

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.