images – Show div if it exists other wise no should be displayed in HTML

Question

I am learning PHP and I am trying to add on featured images. However, if the has not be specified on an image, I don’t want to display in the HTML. Currently the code always outputs in HTML whether it has been set or not.

Any help would be much appreciated. Thanks.

     function get_caption_and_desc_post_image() {
    global $post;
      $thumbnail_id    = get_post_thumbnail_id($post->ID);
    $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
     if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<figcaption>'.$thumbnail_image[0]->post_excerpt.'</figcaption> <div class="image- 
    description">'.$thumbnail_image[0]->post_content.'</div>';
    }
    else {
return; 
        }
       }
      add_filter( 'entry_content_before', 'get_caption_and_desc_post_image' , 9);

0
Gmad 2 months 2021-10-15T18:09:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse