Categories
Code Wordpress

How to Detect If There’s a Featured Image on a WordPress Page or Post

I’m redesigning the website for a Mindfulness Practice Center in New Hampshire, Morning Sun Community, and ran into an issue where I didn’t want the Title on the WordPress page to show if there was a featured image. The layout looks cleaner without the title there (although they opted to have the title appear anyways). The php code I used checks for a header image, then if there is one, do nothing. If there isn’t one, throw the title of the post in there.

<?php
	//Check if there is a header image
	if ( has_post_thumbnail( $post->ID ) ) { ?>
	<?php } else { ?>
		<h1 class="entry-title"><?php the_title(); ?></h1>
	<?php } ?>

The code snippet is pretty useful anytime you want to check for a featured image and do something if there is/isn’t one.

Have fun!

By Jerry Jones

JavaScript Engineer for Automattic, living simply in rural MO.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s