2

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="<?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="<?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="<?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
  • No need to write whole sentences in bold :)
    – kaiser
    Commented Jun 26, 2013 at 20:56

1 Answer 1

4

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
}
3
  • Thanks! That helps! Can you do an and/or, i.e.: if ( is_home() and/or has_post_thumbnail( get_the_ID() ) Commented Jun 26, 2013 at 21:01
  • forgot about the pipes! :D || Commented Jun 26, 2013 at 21:03
  • 1
    @TigerDanky || equals OR, && equals AND and XOR equals "and/or".
    – kaiser
    Commented Jun 26, 2013 at 21:30

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.