• Resolved csleh

    (@csleh)


    Can this:

    <?php if ( in_category(9)) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/brose1.jpg" alt="test system image" />
    <?php } ?>
    <?php if ( in_category(8)) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/automation.jpg" alt="test system image" />
    <?php } ?>

    be made to an if / else?

    I was using this:

    <?php
    if (in_category(9)) {
             echo "<p>we are in category automation</p>";
    } elseif (in_category(8)) {
             echo "<p>we are in category test</p>";
    }  else {
           echo "<p>this is anything else</p>";
    } // That's all, folks!
    ?>

    but putting in a image instead of text (with or without the “echo”) makes the rest of the page go blank everywhere.

    Would like if/else so that I can have a fall back for any page or post without a category.
    Or perhaps the first example could have a line of the php that means “if not in any category, display this image”?

Viewing 2 replies - 1 through 2 (of 2 total)
  • your issue with replacing the text with an image must have been just a punctuation problem — otherwise, it should have worked just fine.

    <?php if (in_category(9)) : ?>
       <img src="<?php bloginfo('template_directory'); ?>/images/brose1.jpg" alt="test system image" />
    <?php elseif (in_category(8)) : ?>
       <img src="<?php bloginfo('template_directory'); ?>/images/automation.jpg" alt="test system image" />
    <?php else : ?>
       [whatever else goes here]
    <?php endif; ?>

    I don’t like splitting up curly brackets like you have – it works, but it’s hard to keep track of, so when I use large amounts of HTML between conditionals, I like to use the alternate syntax, above.

    Thread Starter csleh

    (@csleh)

    You are my hero!

    I knew it didn’t make sense for images to break the rest of the page. I am learning more each time — maybe site 50 I’ll have this code thing down.

    Thanks for your help, works a treat.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘can this be made to an if else statement’ is closed to new replies.