[Resolved] If Featured Image Exists Conditional?

Home Forums Support [Resolved] If Featured Image Exists Conditional?

Home Forums Support If Featured Image Exists Conditional?

  • This topic has 3 replies, 2 voices, and was last updated 6 years ago by Tom.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #528971
    Paul

    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.

    #529341
    Tom
    Lead Developer
    Lead Developer

    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
    }
    #529459
    Paul

    Yep, that does the trick, thanks Tom ๐Ÿ™‚

    #529747
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.