0

I am building a news WordPress theme and using the Description for thumbnails to display who the photo is credited to on the website. I am then placing the credits in the Description on top of the photo using the following code. However, when the photo has no Description/credit, I do not want the description to show because the background color and padding for the Description/credit then create an empty, colored box.

I am hoping that somebody can help me with the proper if statement that will only display the Description/credit if one exists. The description is being pulled via get_post(get_post_thumbnail_id())->post_content in the code below.

<div style="position: relative">
<?php the_post_thumbnail('large');
 echo '<span style="position:absolute; bottom:9px; right:0; color:#FFF; background:rgba(0,0,0,0.8); padding:4px 6px; font-size:0.7em">';
echo get_post(get_post_thumbnail_id())->post_content;
echo '</span>'; ?>
</div>
1

How about like this:

<div style="position: relative">
<?php the_post_thumbnail('large');
$imagedesc = get_post(get_post_thumbnail_id())->post_content;
if(!empty($imagedesc)) {
   echo '<span style="position:absolute; bottom:9px; right:0; color:#FFF; background:rgba(0,0,0,0.8); padding:4px 6px; font-size:0.7em">';
   echo $imagedesc;
   echo '</span>';
} ?>
</div>

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.