WordPress.org

Ready to get started?Download WordPress

Forums

Adding An If/Else Statement (6 posts)

  1. Design Locker
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. Design Locker
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Get rid of the quotes around your Page ids.

  5. Design Locker
    Member
    Posted 2 years ago #

    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">;
    }
    ?>
  6. esmi
    Forum Moderator
    Posted 2 years ago #

    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;?>">

Topic Closed

This topic has been closed to new replies.

About this Topic