If / Else If Custom Function
-
I’m attempting to create an if / else if function for use within my loop. I’m not well acquainted with PHP, so I’m sure there’s a lot wrong here – I’ve never attempted this before.
<!-- START USER-FUNCTION --> <?php /* * function display_category() - calls custom images dependent * on the category assigned to the post. * * NOTE: only works when called from inside the WordPress loop! * * @global object $post */ function display_category() { global $post; ?> <!--Begin Function--> <?php if (in_category('1') ):?> <img src="<?php bloginfo('template_directory') ?>../i/image1.gif" alt='1' /> <?php elseif (in_category('2') ):?> <img src="<?php bloginfo('template_directory') ?>../i/image2.gif" alt='2' /> <?php elseif (in_category('3') ):?> <img src="<?php bloginfo('template_directory') ?>../i/image3.gif" alt='3' /> <?php elseif (in_category('4') ):?> <img src="<?php bloginfo('template_directory') ?>../i/image4.gif" alt='4' /> <?php elseif (in_category('5') ):?> <img src="<?php bloginfo('template_directory') ?>../i/image5.gif" alt='5' /> <?php endif; ?> <!--END Function--> <?php } ?> <!-- END USER-FUNCTION -->
Inside of my wordpress loop, I want to simply do this:
<div id="cat_container"><?php display_category(); ?></div>
That way, this little code calls the function, and displays an appropriate image based upon the category (not the id) for which it is assigned. When this is seen:
<?php if (in_category('1') ):?>
I’m calling the category named 1.I’ve looked at both the codex’s Loop in Action, and this reference in an attempt to get this to work. I’ve tried several variations of the above code, and nothing works. I have gotten the first IF statement to work however, if I include no other elseif code.
Any help would be greatly appreciated.
- The topic ‘If / Else If Custom Function’ is closed to new replies.