Title: Need some help with a php if statement
Last modified: August 19, 2016

---

# Need some help with a php if statement

 *  Resolved [lochmc](https://wordpress.org/support/users/lochmc/)
 * (@lochmc)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/need-some-help-with-a-php-if-statement/)
 * Hi guys,
 * I don’t have the strongest PHP skills and I need a little bit of help with the
   following.
 * Right now I’m using 2 seperate statements like so.
 * 1.
 *     ```
       <?php if (has_post_thumbnail( $post->ID ) ): ?>
       	<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
       	<div id="header" style="background-image:url('<?php echo $image[0]; ?>');">
       	<?php endif; ?>
       ```
   
 * 2.
 *     ```
       <?php if (is_front_page() || is_home()) {?>
       	<div id="header" class="slide">
       	<?php } ?>
       ```
   
 * What I need is for **<div id=”header”>** to be generated _regardless_ of what
   page you’re on. This is a fallback in case the user hasn’t set a featured image.
 * If the page _has_ a featured image then I need the **#header** to have that image
   as it’s background, which I’ve accomplished with the code in #1 above.
 * Then on the homepage, I’m going to have the background image run through a series
   of slides, so I need something like **<div id=”header” class=”slide”>** like 
   I’ve put in #2 above.
 * It’s probably pretty simple. I hope I’ve explained myself cleary enough for someone
   to understand.
 * Thanks for the help.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Jeremy Pry](https://wordpress.org/support/users/jpry/)
 * (@jpry)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/need-some-help-with-a-php-if-statement/#post-1952544)
 * Assuming the code you’ve posted is working correctly for your site, then you’d
   probably want something like this:
 *     ```
       <?php
       $headerAttributes = "";
       if ( has_post_thumbnail( $post->ID ) ) {
           $image = wp_get_attachment_image_scr( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
           $headerAttributes = $headerAttributes . " " . 'style="background-image: url(' . $image[0] . ')"';
       }
       if ( is_front_page() || is_home() ) {
           $headerAttributes = $headerAttributes . ' class="slide"';
       }
       echo '<div id="header"' . $headerAttributes . '>';
       ?>
       ```
   
 * This is just one of many ways to do this, but it should give you enough of an
   idea to tailor it to your own style.
 *  Thread Starter [lochmc](https://wordpress.org/support/users/lochmc/)
 * (@lochmc)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/need-some-help-with-a-php-if-statement/#post-1952826)
 * Thanks for the help, JPry. Perfect! Although you mispelt wp_get_attachment_image__scr_
   🙂
 * Just pasting final code in case anyone else ever needs to use this.
 *     ```
       <?php
       $headerAttributes = "";
       if ( has_post_thumbnail( $post->ID ) ) {
       	$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
       	$headerAttributes = $headerAttributes . " " . 'style="background-image: url(' . $image[0] . ')"';
       }
       if ( is_front_page() || is_home() ) {
       	$headerAttributes = $headerAttributes . ' class="slide"';
       }
       echo '<div id="header"' . $headerAttributes . '>';
       ?>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Need some help with a php if statement’ is closed to new replies.

## Tags

 * [if](https://wordpress.org/support/topic-tag/if/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 2 replies
 * 2 participants
 * Last reply from: [lochmc](https://wordpress.org/support/users/lochmc/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/need-some-help-with-a-php-if-statement/#post-1952826)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
