Forums

[resolved] Conditional Images - Need PHP help (5 posts)

  1. firehouse
    Member
    Posted 4 years ago #

    I want certain pages to have a certain image, the rest of the pages to have a default image if its not those certain pages.

    This is what I have so far, but obviously is massively incorrect as it doesn't work, just breaks my site!

    <div id="wrapper">
    <div id="rightCol">
    <?php
    if (is_page('3,4,6,10,7,8'())include("images/header_home.jpg" alt="title here");  {
    
    } else include("images/header_img.jpg" alt="title here");) {
    
    }
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<h1><?php if (is_frontpage())echo"Accelerated Instrument Ratings<br />& Safety Pilot Services"; else the_title(); ?></h1>
    				<?php the_content(); ?>
    		<?php endwhile; endif; ?>
    </div><!-- end rightCol -->
  2. Kafkaesqui
    Moderator
    Posted 4 years ago #

    First, is_page() can only accept one ID/title at a time. A comma-delimited list will not work.

    Second, include() cannot be used in this way. You want to display the image in your HTML (i.e. <img src=...), not include it like it's a PHP script.

    Here's a method that avoids numerous is_page() tests in your if statement:

    <?php
    $pages_home = array(3,4,6,10,7,8); // 'home' Page ID array
    
    global $post;
    if( in_array($post->ID, $pages_home) ) {
    	$img = 'header_home.jpg';
    	$alt = 'title here';
    } else {
    	$img = 'header_img.jpg';
    	$alt = 'title here';
    }
    ?>
    <img src="images/<?php echo $img; ?>" alt="<?php echo $alt; ?>" />

    For a strictly WordPress Way, use the following:

    <?php
    if( is_page(3) || is_page(4) || is_page(6) || is_page(10) || is_page(7) || is_page(8) ) {
    	$img = 'header_home.jpg';
    	$alt = 'title here';
    } else {
    	$img = 'header_img.jpg';
    	$alt = 'title here';
    }
    ?>
    <img src="images/<?php echo $img; ?>" alt="<?php echo $alt; ?>" />
  3. firehouse
    Member
    Posted 4 years ago #

    I'll give it a spin see what I come up with. Thanks for the reply. In the morning I'll post my outcome! Thanks for helping, nice, clear instructions!!

  4. firehouse
    Member
    Posted 4 years ago #

    ok, tried it, and it works splendidly!

    Can I bother you for one more favor, lets say I want to add more if to it. such as. If is home, use this image, if is page 4 use this image, if is page 7 use this image, if is anything else, use this default image... in other words, to add more ifs to it. What do I need to add to do that part?

    You're a help! Thanks!

  5. firehouse
    Member
    Posted 4 years ago #

    oh, and I'm using the strictly WordPress way.

Topic Closed

This topic has been closed to new replies.

About this Topic