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'm setting up my wordpress website on my server now and have the rotating images on the home page and it's beautiful, but now I want to have a conditional tag telling wordpress if it's not the home page use the featured images per each page.

This is the code I've placed onto my home page to display my plugin rotating banners from iThemes.com

<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('banner-widget')) : else : ?>  
  <p><img class="banner" src="<?php bloginfo('template_directory') ?>/img/BANNER1-averylawoffice.jpg" alt="Banner 1"></p>
<?php endif; ?>

It works great! My widget is ready to go for this, images are all there and it works. Now I have this code I want to put into an if else statement so if the page ISN'T the home page, show the featured image.

I'm no programmer expert and have looked around a lot to get this code, however it doesn't work and I just see a blank spot on the home page and the images on the rest of the page as they should be.

<?php if (is_home()) {
    <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('banner-widget')) : else : ?>  
        <p><img class="banner" src="<?php bloginfo('template_directory') ?>/img/banner.jpg" alt="Banner 1"></p>
    <?php endif; ?>
} else { ?>
    <div id="banner"><?php the_post_thumbnail( 863, 328 );?></div>
<?php } ?>

What am I doing wrong is order to not get the desired effect I'm looking for? I know this is simple so I don't want to use a useless plugin to do this and slow down the site as it's going to be slow as is.

Any help or suggestions would be great!

share|improve this question

3 Answers 3

up vote 1 down vote accepted

There are multiple issues that I think you want to adjust:

  1. You open an extra PHP tag before the second if statement. That shouldn't be there.
  2. Make sure that is_home() is what you want. If you have a static front page (set in Settings > Reading), then you actually want is_front_page(). Think of is_home() as is_blog() (to be clear, is_blog() does not exist).
  3. If you want to give the_post_thumbnail() a width and height, it needs to be in an array. Your other option is to define an image size in functions.php and then call that there.
  4. I think your HTML can be cleaner by just always wrapping the banner in the #banner div.
  5. Just to be sure that you always have an image, I would check to make sure that you have a post thumbnail set and make sure you have a fallback.

With all those suggestions wrapped into code, here's what I suggest:

In your functions.php add:

add_image_size( 'banner', 863, 328, true );

And wherever your banner snippet is:

<!-- Let's always wrap our banner in the banner div for easy styling -->
<div id="banner">
<?php

// Check to see if we're on a non-home-page and if the non-home-page has a featured image. use is_front_page() if that's desired
if( !is_home() && has_post_thumbnail() ) {

    get_the_post_thumbnail( 'banner' );

// if we're on the home page OR don't have a featured image but DO have a dynamic sidebar
} elseif ( function_exists('dynamic_sidebar') && is_dynamic_sidebar('banner-widget') ) {

    dynamic_sidebar('banner-widget');

// no featured image or on homepage with no sidebar, spit out a static image
} else {
    echo '<img class="banner" src="' . get_template_directory_uri() . '/img/banner.jpg" alt="Banner 1">';
}
?>

UPDATE:

That final echo statement was previously a complete mess. Sorry! I cleaned it up and I think it should work now. I also replated the bloginfo() function (which is now deprecated) with the new, preferred equivalent function.

share|improve this answer
    
This is a beautiful example, however I've placed it in and I now have syntax error but I don't understand what ";" it's needing. I tried it on my local server before hand and the whole site just displayed a white background and I couldn't view anything. I've done what you've said here, placed everything in a banner div, added that line to the functions.php file. I love this fall back option just to cover all bases so after realizing why it wasn't working before now I'd love to incorporate this fall back method but not sure why it's not working. You can view the site again and see what I mean. –  kia4567 Jun 1 '12 at 21:26
    
Hi @Amber. My bad. Try it again. –  mrwweb Jun 1 '12 at 22:53
    
Perfect! Thanks so much! One thing though, maybe you can help me on it. I'm testing it out and I'm deliberately not setting a featured image so I can see what shows up. The rotating banner shows up instead of the static image. Isn't that what the else is for? My img is there but it's not calling it. –  kia4567 Jun 1 '12 at 23:46
    
That statement is setup to show the rotating banner as the first fallback for a page without a featured image (try to follow each condition and understand it fully). If you want it to skip the second condition add && is_home() to the elseif condition. –  mrwweb Jun 2 '12 at 1:05
    
Will do. Thanks –  kia4567 Jun 2 '12 at 2:05

You might want to try is_front_page() instead of is_home(). is_home() returns true when your most recent posts are being displayed on your home page; is_front_page() returns true if you've set it to be a static page in Settings > Reading.

See this article for more info: http://codex.wordpress.org/Conditional_Tags#The_Conditions_For_...

Hope this helps!

share|improve this answer
    
too fast for me ;-) –  Zach L Jun 1 '12 at 16:02
    
This is definitely important to keep in mind, will cause issues when using a static front page, and is something that I find myself constantly pointing out to people. But, I don't think it's causing the issue the OP is observing. –  Chip Bennett Jun 1 '12 at 16:06
    
This is great to know! I didn't know that. I keep reading as much as I can about wordpress but sometimes there is so much you don't know where to begin or how to go about asking for what you need or even finding it on that site. This worked! Thanks Michelle! –  kia4567 Jun 1 '12 at 21:23

If the code in the question is exactly as it is in your Theme, the only real problem I see is a PHP syntax error:

<?php if (is_home()) {
    <?php 

You're opening a PHP tag when you've already got an open PHP tag.

Try removing that second PHP tag.

share|improve this answer

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.