0

I have the following code at the start of my single.php loop that displays the post featured image as a background.

<?php  $featured_background = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); ?>    

<div class="parallax" id="parallax1" style="background: url(<?php echo $featured_background['0'];?>)  !important; background-position: 50% 50% !important; background-repeat: no-repeat !important; background-size: cover !important; background-attachment: fixed !important;" data-stellar-background-ratio=".5">
        <div class="parallax-content">
            Some content            
        </div>
</div>

I've set .parallax to be 600px high in the stylesheet.

Of course this poses a problem for posts that don't have a featured image.

It seems then that I need a conditional statement that only displays the code above if the post has a thumbnail.

I've looked at the wordpress codex, but am unsure how to apply the teachings there to my case. It also looks like they'd be a lot of use of the echo command due to all the HTML.

I think I understand the first part of the statement which will be something like:

<?php

if ( has_post_thumbnail() ) {
    echo'my html code as above'
}
else {
    just get on with rendering the rest of the page
}
?>

But then I don't understand how to tell it to just get on with rendering the page in the else statement.

0

Although this is not a WordPress specific question:

There is no need to tell it to get on with rendering the rest of the page. The else portion should contain what code you want to include if there is not a featured image. If you just don't want to render that portion of the page then there is no need for the else.

Hopefully this makes some sense:

<header></header>
<if statement>
    parallax div
</if statement>
<content.. etc>

Any code outside of the if statement will run whether the if statement is true or false.

Also, you do not need to echo all of the HTML code if you close the php tag prior to outputting your information.

<?php if ( has_post_thumbnail() ) { ?>
    Plain HTML code here.
<?php } else { ?>
    What happens if there is no post thumbnail.
<?php } ?>
  • OK so the rest of the page doesn't need to sit within <?php } else { ?><?php } ?> and I just leave that blank. Seems to work. Thanks. – C-M Mar 27 '15 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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