Site logo

Archived topic

If Featured Image Exists Conditional?

3 replies · Started by Paul on March 24, 2018

Viewing posts 1–4 of 4

Hi, I just added this code, which works great:

//Insert 'written by' author box after featured image
add_action( 'generate_before_entry_title', 'tu_after_featured_image', 15 );
function tu_after_featured_image() {
  ?>
    <div class="meta-author">
      <div class="meta-author-img">
        <img src="#" width="100" height="100" alt="Paul" />
      </div>
      <div class="meta-author-wrapped">
        Written by <a href="#">Paul</a>
      </div>
    </div>
  <?php
}

But it shows irregardless of whether there actually is a featured image, and when there isn't and the above is still displayed it looks off, overlapping the navigation.

So what I'd like to do is add a conditional to the above code, to only execute it when a post has a featured image, and not when there isn't any featured image set.

How can I achieve this?

P.S. The 'Your website URL' post linked there has a featured image. To see a post without a featured image, and how it looks, please click on 'Hanoi Home' in the nav.

Hey Paul,

Try this:

add_action( 'generate_before_entry_title', 'tu_after_featured_image', 15 );
function tu_after_featured_image() {
  if ( ! has_post_thumbnail() ) {
    return false;
  }
  ?>
    <div class="meta-author">
      <div class="meta-author-img">
        <img src="#" width="100" height="100" alt="Paul" />
      </div>
      <div class="meta-author-wrapped">
        Written by <a href="#">Paul</a>
      </div>
    </div>
  <?php
}

Yep, that does the trick, thanks Tom :)

You're welcome :)

This archived topic is closed to new replies.