• andrew55

    (@andrew55)


    I have a php snippet (inserted in php widget) which I am trying to use to display a different image depending on what category a page is on:

    <?php if ( is_category(category1) ) { ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category(category2) ){ ;?>
    <?
    echo '<img src="/images/image2.jpg">';
    ?>
    <?php } elseif ( is_category(category3) ){ ;?>
    <?
    echo '<img src="/images/image3.jpg">';
    ?>
    <?php } elseif ( is_category(category4) ){ ;?>
    <?
    echo '<img src="/images/image4.jpg">';
    ?>
    <?php } else { ;?>
    <?php } ;?>

    I got the idea from here: http://wordpress.org/support/topic/different-header-image-not-random-for-each-category

    Seems like it should work, but it doesn’t. Any suggestions in the proper syntax? Thank you for any help.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Evan Herman

    (@eherman24)

    I think you want to do something like:

    <?php if ( is_category('1') ) { ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category('2') ){ ;?>
    <?
    echo '<img src="/images/image2.jpg">';
    ?>
    <?php } elseif ( is_category('3') ){ ;?>
    <?
    echo '<img src="/images/image3.jpg">';
    ?>
    <?php } elseif ( is_category('4') ){ ;?>
    <?
    echo '<img src="/images/image4.jpg">';
    ?>
    <?php } else { ;?>
    <?php } ;?>

    or instead you can use the category slug name, instead of the ID value. (I prefer the category ID value, just incase the slug or category name ever changes)

    <?php if ( is_category('category1') ) { ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category('category2') ){ ;?>
    <?
    echo '<img src="/images/image2.jpg">';
    ?>
    <?php } elseif ( is_category('category3') ){ ;?>
    <?
    echo '<img src="/images/image3.jpg">';
    ?>
    <?php } elseif ( is_category('category4') ){ ;?>
    <?
    echo '<img src="/images/image4.jpg">';
    ?>
    <?php } else { ;?>
    <?php } ;?>
    Thread Starter andrew55

    (@andrew55)

    Evan – thanks for the reply. I put this snippet in there (using category ids), but still am getting nothing where image should be:

    <?php if ( is_category('12') ) { ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category('13') ){ ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category('16') ){ ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } elseif ( is_category('15') ){ ;?>
    <?
    echo '<img src="/images/image1.jpg">';
    ?>
    <?php } else { ;?>
    <?php } ;?>
    Evan Herman

    (@eherman24)

    I think your issue is all of the semi colons and open/closing php statements that shouldn’t be there. Try this:

    <?php
    if ( is_category('12') ) {
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('13') ) {
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('16') ) {
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('15') ){
      echo '<img src="/images/image1.jpg">';
     } else {
     // nothing
    }
    ?>

    my mistake for missing that before. The above is correct.

    Evan Herman

    (@eherman24)

    The above should be correct now.

    Thread Starter andrew55

    (@andrew55)

    Thanks for the help, but no success yet. I tried your last suggestion, and I also tried the following:

    <?php
    if ( is_category('12') ) {
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('13') ){
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('16') ){
      echo '<img src="/images/image1.jpg">';
    } elseif ( is_category('15') ){ ;?>
      echo '<img src="/images/image1.jpg">';
     } else {
     // nothing
    }
    ?>

    It’s seems like one of the variations should work. Still nothing shows up in the place where image should be. From source code view, there isn’t anything there. Thank you for any suggestions.

    Evan Herman

    (@eherman24)

    Are you putting this inside of the content area in a new page or post? Or are you adding this directly to a .php template file?

    Thread Starter andrew55

    (@andrew55)

    I trying in a php widget, and directly in the header.php file.

    Evan Herman

    (@eherman24)

    I don’t think you can write PHP inside of a widget. You need to download a plugin like Exec php to allow php to be excuted inside widgets/posts/pages.

    However, it should work in header.php

    Are you seeing any 404 errors inside of the developer console?

    Thread Starter andrew55

    (@andrew55)

    This is the widget plugin I am using, which should allow me to use php in widget:
    http://wordpress.org/plugins/php-code-widget/

    I will try the plugin you suggested.

    No 404s that I can see in developer console.

    Evan Herman

    (@eherman24)

    Go ahead and test this out, which should work. Just test it using strings instead of image, and then replace the string with your image URL when you know its working.

    <?php
    if ( is_category('12') ) {
      echo 'category 12!';
    } else if ( is_category('13') ){
      echo 'category 13!';
    } else if ( is_category('16') ){
      echo 'category 16!';
    } else if ( is_category('15') ){
      echo 'category 15!';
     } else {
      echo 'No category!';
    }
    ?>

    That should print out Category # inside the header when you click on the appropriate page.

    Evan Herman

    (@eherman24)

    This is tested and working on my localhost installation inside of header.php:

    <?php
    	global $post;
    	if ( in_category('2', $post->ID) ) {
    	  echo 'category 2!';
    	} elseif ( is_category('3', $post->ID) ) {
    	  echo 'category 3!';
    	} elseif ( in_category('4', $post->ID) ) {
    	  echo 'category 4!';
    	} else {
    	  echo 'NO CATEGORY!';
    	}
    ?>

    Michael

    (@alchymyth)

    https://codex.wordpress.org/Function_Reference/is_category

    are you aiming for category archive pages?

    what is your result?

    can you post link to your site where the result could be investigated?

    where are your images located?

    have you tried to use absolute image urls?

    Thread Starter andrew55

    (@andrew55)

    Code is working great now. Images show up great, without using absolute image urls. Thank you very much.

    Evan Herman

    (@eherman24)

    No problem at all. Sorry it took a bit to get ironed out. There’s really two ways of achieving it. Wasn’t sure how you wanted to go about it.

    After setting it up locally I was able to get it resolved fairly quickly.

    Happy WordPress-ing!

    Thread Starter andrew55

    (@andrew55)

    Your apologizing? Yea right! Thank you again.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘image based on catagory’ is closed to new replies.