• I dont know PHP and looked for the answer, but couldn’t find it. I am trying to create an if statement where it displays a certain image, unless I specify a different image for a certain category.

    I have this:

    <?php if (in_category('1') ): ?>
    <img src='/images/teams.jpg' />
    <?php elseif in_category('4') ): ?>
    <img src='/images/child-sponsorship.jpg' />
    <?php else: ?>
    <img src='/images/home-image.jpg' />
    <?php endif; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php
    if (in_category(‘1′)) echo “<img src=’/images/teams.jpg’ />”;
    elseif (in_category(‘4′)) echo “<img src=’/images/child-sponsorship.jpg’ />”;
    else echo “<img src=’/images/home-image.jpg’ />”;?>

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    I hate using if followed by colons. Hard to read and understand.. Bah. I’m also not a big fan of continually opening and closing the PHP blocks every other line.

    Try this instead:
    <?php
    if (in_category('1')) {
    echo "<img src='/images/teams.jpg' />";
    }
    else if (in_category('4')) {
    echo "<img src='/images/child-sponsorship.jpg' />";
    }
    else {
    echo "<img src='/images/home-image.jpg' />";
    }
    ?>

    See how much nicer that is? Spaced out, easy to read, groups the bits together naturally… just nicer, IMO. Works too. ??

    Thread Starter jefft

    (@jefft)

    Thanks for all your help! But I’m still having a problem…the home page displays does not display the “else” image, but rather, the image from the corresponding category of the first post. Suggestions?

    Thread Starter jefft

    (@jefft)

    Not first post, but most recent post. (The first one WP displays.)

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    jefft: Add an “if (is_home()) { blah blah }” to the beginning of the series of if statements. That way, you can explicitly define what you want on the home page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PHP Help’ is closed to new replies.