1

I am using this code in my template:

<?php
if (has_post_thumbnail()) {
    the_post_thumbnail(array(80,80));
} else {
    ?>
    <img src="http://example.org/<?php the_title(); ?>" width="80px" height="80px" alt="<?php the_title(); ?>">
    <?php
} ?>

its supposed to post an image in case the featured image is not present but it doesnt, like if it always detects a featured image even when there is none. So I guess has_post_thumbnail() is always giving true.

How can I fix this? Or maybe there is an alternative to detect whether or not there is a feature image present?

Thank you.

3
  • What does it output when you load an image that's not supposed to have a post thumbnail/featured image? And what kind of loop is this inside?
    – Tom J Nowell
    Commented May 11, 2018 at 1:15
  • 1
    As mentioned in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE."
    – Johansson
    Commented May 11, 2018 at 1:25
  • It outputs nothing and its inside while ( have_posts(). Also, the post contains no image at all.
    – Cain Nuke
    Commented May 11, 2018 at 2:07

1 Answer 1

3

Then you're using it outside of the loop, try this instead:

<?php
if (has_post_thumbnail( get_the_ID() )) {
    get_the_post_thumbnail( get_the_ID(), array(80,80) );
} else {
    ?>
    <img src="http://example.org/<?php the_title( get_the_ID() ); ?>" width="80px" height="80px" alt="<?php the_title( get_the_ID() ); ?>">
    <?php
} ?>

If you're not inside of a WP_Query loop, then post-specific functions like has_post_thumbnail() the_title() won't work unless you explicitly pass a the unique-id of a post/page to them.

9
  • It is inside while ( have_posts() but its not working. If the featured image is present it displays that but if not then nothing its displayed.
    – Cain Nuke
    Commented May 11, 2018 at 3:11
  • Why are you using img src="http://example.org/<?php the_title(); ?>" in the else? How would that ever map to an image? Is the title of the post/page the same as an image which resides at the root of your website?
    – admcfajn
    Commented May 11, 2018 at 16:46
  • its a little more complex than that but yes, its about that.
    – Cain Nuke
    Commented May 11, 2018 at 17:05
  • By the way, I managed to make it work though I am aware it might not be the best solution ob_start(); the_post_thumbnail(); $ass = ob_get_contents(); ob_end_clean(); if ($ass!=null) { the_post_thumbnail(array(80,80)); } else
    – Cain Nuke
    Commented May 11, 2018 at 17:21
  • Nice :) $ass = get_the_post_thumbnail(); if(!empty($ass)) might avoid the need for buffering.
    – admcfajn
    Commented May 11, 2018 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.