• 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I wonder if using numbers for category names is causing trouble. The in_category function can take both category IDs (numbers) and category names (strings) as arguments. I think it is possible that it is interpreting ‘1’ as an ID, and not a name. You might try testing with non-numeric names to rule out this possibility.

    Thread Starter kokicf

    (@kokicf)

    I put the numbers there because I was replacing sensitive data (it’s there as an example). That’s why there are so many numbers. In my final code, I’d have a more realistic name, like “photography” for example. Sorry for the confusion.

    OK – that was just a wild guess anyway.

    Is it just the if tests that are not working? Try using elseif (true) for one of the cases (say number 3) and see if the correct image for that case is put into the page.

    If that works, something is not working with in_category(). Dump $post with print_r to see if it contains what you are expecting.

    Just dump everything to the screen whilst writing the code, once it works, then remove the stuff you don’t need…

    No point guessing when you can just dump/print the data, var_dump, print_r, whatever you prefer..

    Might also be worth chucking a few basic things in like..

    if( in_the_loop() ) echo 'In the loop!';
    
    if( in_category('1') ) echo 'Houston we have a category!';
    
    if( in_category('2') ) echo 'Houston we have another category!';

    Silly, but obvious things, that let you know what’s working and what’s not… don’t worry about making it pretty, until it works… ??

    Thread Starter kokicf

    (@kokicf)

    Just dump everything to the screen whilst writing the code

    How do I do that? My knowledge of php is limited to what I find in the WordPress Codex’s template tags section at the moment.

    You could use t31os_’s statements, or print_r($post); just below the Begin Function comment.

    You have never explained what you are getting when the code doesn’t work. Do you get any output at all for any categories?

    Did you try my suggestion about changing one of the in_category(‘n’) tests to true? Did that make a difference?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘If / Else If Custom Function’ is closed to new replies.