• Resolved Jintro

    (@jintro)


    I’ve been looking for code to show the feature image in a div so i can control the styling of that image.
    I am not good at PHP, but I try to fiddle with code snippets I find here and elsewhere. But I’ve been braeking my head on this one:

    The code below works great. But I wonder how I can adjust it so that the div doesn’t show when its empty. I don’t have this on a live site yet, sorry. But I also used some CSS to style the div, such as a border.

    All help and suggestions are welcome.

    <?php
    
                $thumb = get_post_thumbnail_id();
                $img_url = wp_get_attachment_url( $thumb,'full' );
    
                ?>
    
                <div class="blogfeat">
                    <img class="blogfeatimg" src="<?php echo esc_url($img_url); ?>"/>
                </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Just add a conditional statement.

    <?php
    
                $thumb = get_post_thumbnail_id();
                $img_url = wp_get_attachment_url( $thumb,'full' );
    
                if ($img_url) {?>
    
                <div class="blogfeat">
                    <img class="blogfeatimg" src="<?php echo esc_url($img_url); ?>"/>
                </div>
    <?php }?>
    Thread Starter Jintro

    (@jintro)

    I had the markup of the if-statement wrong.
    This works perfect. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘featured image PHP if loop – no empty div’ is closed to new replies.