Forums

[resolved] If statement for pages (4 posts)

  1. RyuuJinSilver
    Member
    Posted 2 years ago #

    Okay, I am trying to make it so I can show a different image depending on what page I am on. I have tried this if statement.

    <?php
    if (is_home()) {
     echo'<img src="locationofimage" alt="" />'
    } elseif (is_page('2')) {
     echo'<img src="locationofimage" alt="" />'
    } else {
     echo'<p>Just a Test</p>'
    }
    ?>

    But when I do this nothing shows up on the page at all (completely white)

    However if I just put the image path with no PHP

    <img src="locationofimage" alt="" />

    The image shows up just fine. Any suggestions.

    BTW, yes I have done a search for similar questions, which is where I got this idea in the first place to no avail.

  2. Dunam
    Member
    Posted 2 years ago #

    You might want to look if your if statement is working at all. I`m unfamiliar with the 2 functions you call.

    Also, check if echo '<img src="locationofimage" alt="" />' works. Then if it doesnt, press control+u and see what url its really trying to find the image at.

  3. RyuuJinSilver
    Member
    Posted 2 years ago #

    Thanks for the quick responce.

    That's the thing I don't think the if statement is working. I guess I was wondering if there was an alternative way to display a different image depending on the page id.

    Also I checked the echo, and it does work.

  4. RyuuJinSilver
    Member
    Posted 2 years ago #

    I've figured it out! Thanks for your help. For anyone who wants to try this is works like this.

    <?php if (is_page(2)) { ?>
    
        <img src="location of image.png" alt="" />
    
        <?php } elseif (is_page(3)){ ?>
    
        <p>Whatever you want</p>
    
        <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic