I’ve got a situation where I need to find out if the currently loaded post has a featured image, if it does, I want it to display a different logo than if it doesn’t have a featured image associated with the post.
This works right now, but doesn’t have the check feature I’m looking for:
<?php echo is_front_page() ? '<h1 id="logo" class="h1 threecol first">' : '<strong id="logo" class="h1 threecol first">'; ?>
<a href="https://wordpress.stackexchange.com/questions/104398/<?php echo home_url(); ?>" rel="nofollow"><img src="<?php header_image() ?>" alt="<?php bloginfo('name'); ?> logo" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" /></a>
<?php echo is_front_page() ? '</h1>' : '</strong>'; ?>
My issue is that I’ve looked at the WP documentation for get_posts or starting a new WP_query, but haven’t had any luck understanding how I can:
- run a new loop that gets the page ID and sees if it has a featured image
- do a standard if/else statement afterwards
My thinking with the if/else would be like this:
<?php echo is_front_page() ? '<h1 id="logo" class="h1 threecol first">' : '<strong id="logo" class="h1 threecol first">'; ?>
<a href="https://wordpress.stackexchange.com/questions/104398/<?php echo home_url(); ?>" rel="nofollow">
<? // IF/ELSE CODE TO EXECUTE IF post_has_thumbnail ?>
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php header_image() ?>" alt="<?php bloginfo('name'); ?> logo" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" />
<?php else : ?>
<img src="/i/logo-no-feat-img.png" alt="<?php bloginfo('name'); ?> logo" height="120" width="222" />
<?php endif; ?>
<? // end ATTEMPTED IF/ELSE CODE ?>
</a>
<?php echo is_front_page() ? '</h1>' : '</strong>'; ?>
WORKING CODE
<?php echo is_front_page() ? '<h1 id="logo" class="h1 threecol first">' : '<strong id="logo" class="h1 threecol first">'; ?>
<a href="https://wordpress.stackexchange.com/questions/104398/<?php echo home_url(); ?>" rel="nofollow">
<? // IF/ELSE CODE TO EXECUTE IF post_has_thumbnail ?>
<?php if (is_home() || has_post_thumbnail(get_the_ID()) ) : ?>
<img src="<?php header_image() ?>" alt="<?php bloginfo('name'); ?> logo" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" />
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/library/images/logo-without-feat_img.png" alt="<?php bloginfo('name'); ?> logo" height="302" width="203" />
<?php endif; ?>
<? // end IF/ELSE CODE ?>
</a>
<?php echo is_front_page() ? '</h1>' : '</strong>'; ?>
1 Answer
has_post_thumbnail()
accepts a post ID:
if ( is_singular() and has_post_thumbnail( get_the_ID() )
{
// show post thumbnail
}
elseif ( is_front_page() )
{
// show front page content
}
else
{
// do something else
}