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.

In short, Im not a master of PHP yet and Im not entirely sure how to place a series of checks (if statements) in my template so that it will know what to display depending on what the client uploads/fills out in the CPT ive created...

What Im trying to have it do is:
(1) Check to see if there is a URL entered (custom meta box) - if so, display it and wrap it around the featured image...
(2) If no URL is chosen - then only display the featured image by itself...
(3) and lastly, if no banner image is created at all - then display a default image....

Thanks for the help!

Here is what I have thus far which is currently not working:

<?php if (get_post_meta($post->ID, 'meta_box_topbanner_url', true))?>
    <a href="<?php echo get_post_meta ($post->ID, 'meta_box_topbanner_url', true); ?>" target="_blank">
       <?php the_post_thumbnail('top-banner-img', array('class' => 'top-banner-img', 'title' => '')); ?>
    </a>

<?php elseif the_post_thumbnail('top-banner-img', array('class' => 'top-banner-img', 'title' => '')); ?>

<?php else '<img src="images/epr_bannder_default.jpg" alt="epr_bannder_default" width="960" height="200" />' ?>
share|improve this question
    
Can you clarify what isn't working? What is the actual output that you get from this code? –  Chip Bennett Apr 18 '12 at 19:02
    
Hi, it actually ends up breaking the entire page but I cant pinpoint where in my code is causing it. I think its possibly my elseif but like I said I just pieced this together the best I could without knowing the correct conventions for writing it. –  Mr.Brown Apr 18 '12 at 19:06
1  
Well, we need to know if it is a PHP syntax issue - in which case the question is out of scope for WPSE - or if it is a WordPress-specific issue, such as usage of get_post_meta(), etc. –  Chip Bennett Apr 18 '12 at 19:12
    
Not sure, but I believe the root of my problem lies more in using correct PHP writing conventions rather than anything else...but being that this is directly related to my wordpress template I didnt think it was inappropriate to ask here. –  Mr.Brown Apr 18 '12 at 19:43
add comment

1 Answer 1

up vote 1 down vote accepted

You've got some basic syntax problems. Your elseif statement wasn't actually checking anything and wasn't properly wrapped in parenthesis. The logic more appropriately checks if the first condition is true, then if the second condition is true, and finally if all else fails, display the hard coded image.

<?php $meta_box_topbanner_url = get_post_meta( $post->ID, 'meta_box_topbanner_url', true ); ?>
<?php $post_thumbnail = get_the_post_thumbnail( $post->ID ); ?>

<?php if ( !empty( $meta_box_topbanner_url ) ) : ?>

    <a href="<?php echo get_post_meta ($post->ID, 'meta_box_topbanner_url', true); ?>" target="_blank">
        <?php the_post_thumbnail('top-banner-img', array('class' => 'top-banner-img', 'title' => '')); ?>
    </a>

<?php elseif ( !empty( $post_thumbnail ) ) : ?>

    <?php the_post_thumbnail('top-banner-img', array('class' => 'top-banner-img', 'title' => '')); ?>

<?php else : ?>

    <img src="images/epr_bannder_default.jpg" alt="epr_bannder_default" width="960" height="200" />' ?>

<?php endif; ?>

EDIT

I removed a redundant check from the elseif condition.

share|improve this answer
    
Would you mind adding some descriptive text explaining what is different about this code? –  Chip Bennett Apr 18 '12 at 19:13
    
Yep, that seems to work @develpdaly, though the last check is not grabbing the backup image. (I checked and my image path is correct for the record)... Additionally, is there a more consolidated way to do this or is this my only option? –  Mr.Brown Apr 18 '12 at 20:06
    
I think that's about as simple as you'll get it. Check line 2 where I setup get_the_post_thumbnail() because you may need to make some changes to that function's arguments. It is simply getting the thumbnail without any restrictions. –  developdaly Apr 18 '12 at 20:17
    
hmm...well my first thought would be that the default image is not actually a featured image/thumbnail so I suppose that would be the first logical reason for it not to grab the appropriate image if thats how your function is written to work. This image simply resides in my images folder. –  Mr.Brown Apr 18 '12 at 23:04
add comment

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.