• Hi everyone,
    I’ve searched through many forum posts on the subject, but still can’t seem to get this to work. I’m trying to have an image display differently on some pages. Here’s my code:

    <?php
    if (is_page(information)) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/information.png">
    <?php } elseif (is_page(array(about-us,contact))) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/about.png">
    <?php } elseif (is_page(array(products,coming-soon))) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/products.png">
    <?php } else { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/default.png">
    <?php } ?>

    The first couple of images work fine (for the first “if” statement and “elseif” statement), but for the second “elseif” it doesn’t work. By the way, I am using the bloginfo(‘template_directory’) code to resolve an issue I had linking back to the source image folder. Any suggestions on getting these conditional statements to work?

Viewing 5 replies - 1 through 5 (of 5 total)
  • what exactly ‘doesn’t work’?

    where is the code inserted?

    what is the result for pages ‘products’ and ‘coming-soon’ ?

    do these pages use page templates?

    can you post a link where this can be seen?

    Thread Starter jfarman

    (@jfarman)

    The last pages (products, coming-soon) don’t display the image requested in the conditional tag, but instead display the first image (information.png) which also seems to have become the default image for the rest of the pages not included in this bit of code.

    The code is placed in the middle of my page.php file, inside of a div. Unfortunately, I can’t link you to it because the site is not live yet (I’m using MAMP to run the site locally).

    just checking again:
    all these pages are ‘ordinary’ static pages, therefore using page.php, and do not use page templates?

    are there any possible interfering queries or widgets or plugin codes before your lines?

    you could paste the full code of page.php into a http://wordpress.pastebin.com/ and post the link to it here.
    someone might check it and might be able to make some suggestions.

    Thread Starter jfarman

    (@jfarman)

    Thanks for your help. I think I found a workaround that’s functioning. Instead of conditional statements for this, I’m instead using custom fields. So my code looks like this:

    <div id="main-img">
    <img src="<?php bloginfo('template_directory'); ?>/images/information.png" class="centeredImage" />
    
    <?php if (is_page()) { ?>
    <?php $image = get_post_meta($post->ID, 'mainimg', true); ?>
    <img src="<?php echo $image; ?>" class="centeredImage" />
    <?php } ?>
    </div>

    So, this way, the “information.png” image is a default on any page that doesn’t have the “mainimg” custom field specified. I know there is an easier way to do this with conditional tags, but I’m far too new at this to know!

    well done 😉

    please mark the thread as ‘resolved’, if this is ‘sorted’.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Conditional tags: using "elseif" with images’ is closed to new replies.