Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>'; ?>
share|improve this question
    
No need to write whole sentences in bold :) –  kaiser Jun 26 '13 at 20:56
    
jaja, thanks @kaiser –  Tiger Danky Jun 26 '13 at 21:04

1 Answer 1

up vote 0 down vote accepted

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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