Title: Adding An If/Else Statement
Last modified: August 20, 2016

---

# Adding An If/Else Statement

 *  [Design Locker](https://wordpress.org/support/users/design-locker/)
 * (@design-locker)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/)
 * Hello All,
 * When I open a particular page I want wordpress to check the page ID and if it
   is a certain page ID then insert “image1”. If page ID equals another predefined
   ID then insert “image2” etc.
 * In other words;
 * If page ID equals 15, insert image1.jpg
    Else If page ID equals 16, insert image2.
   jpg Else If page ID equals 17, insert image3.jpg etc…
 * Generally I need the code to be able to achieve this as I really am not all that
   good at PHP.
 * I look forward to your help and thank you.

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400603)
 * [http://codex.wordpress.org/Conditional_Tags](http://codex.wordpress.org/Conditional_Tags)
 *  Thread Starter [Design Locker](https://wordpress.org/support/users/design-locker/)
 * (@design-locker)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400608)
 * Thanks for reply. Have spent hours looking at this page, my PHP is non-existent
   and below is all I can come up with;
 *     ```
       <?php
       if ( is_page( '15' ) || '2' == $post->post_parent ) {
       <img src="image1.jpg">;
       } elseif ( is_page( '16' ) || '56' == $post->post_parent ) {
       <img src="image2.jpg">;
       } else {
       <img src="image3.jpg">;
       }
       ?>
       ```
   
 * Thanks for help.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400611)
 * Get rid of the quotes around your Page ids.
 *  Thread Starter [Design Locker](https://wordpress.org/support/users/design-locker/)
 * (@design-locker)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400614)
 * thanks esmi; do you think this looks right?
 *     ```
       <?php
       if ( is_page(15) {
       <img src="image1.jpg">;
       } elseif ( is_page(16) {
       <img src="image2.jpg">;
       } else {
       <img src="image3.jpg">;
       }
       ?>
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400621)
 * Your logic is correct but your syntax is all wrong. Try:
 *     ```
       <?php if ( is_page(15) ) $img = 'image1.jpg';
        elseif ( is_page(16) ) $img = 'image2.jpg';
        else $img = 'image3.jpg';
       if( isset( $img) ) ?><img src="<?php echo $img;?>">
       ```
   

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

The topic ‘Adding An If/Else Statement’ is closed to new replies.

## Tags

 * [if statement](https://wordpress.org/support/topic-tag/if-statement/)
 * [Page ID](https://wordpress.org/support/topic-tag/page-id/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [esmi](https://wordpress.org/support/users/esmi/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/adding-an-ifelse-statement/#post-2400621)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
