Shorthand conditionals not working

I’m working locally and am in a while loop.

Non of my posts have featured images however this conditional doesn’t output the placeholder span

{!! has_post_thumbnail() != false ? get_the_post_thumbnail(get_the_ID(), 'large') : '<span class="placeholder"></span>' !!}

any clue as to why not?

So it turns out has_post_thumbnail() always returns true which isn’t very helpful.

A workaround I put in place was this…

{!! ($featured_image = get_the_post_thumbnail(get_the_ID(), 'large')) ? $featured_image : '<span class="placeholder"></span>' !!}

My guess is that you are outside the loop and have to do has_post_thumbnail(get_the_ID()) for the code to work the way you want. I am basing that on the way you are calling get_the_post_thumbnail().

I was definitely in a while loop but after testing what has_post_thumbnail gave pack I found it was true no matter if the post has a featured image or not

This topic was automatically closed after 42 days. New replies are no longer allowed.