If else if else while if else php for wordpress.

7 replies
I want to create some code for my wordpress blog sidebar. I want it to display a different offer depending on category. There are over 7 different categories I wish to do this with.

Is it possible to work with if else and while over 7 different variables, the categories in WP being the variable.

If it is I'm willing to pay for someone to create it for me.

Thanks
#dor #php #wordpress
  • Profile picture of the author shaddai
    Executable PHP widget

    That should get you to the point of self coding php in the side bar.

    Worthless link...

    don't know if it'll help. Maybe enough to get you on the right path.

    Of course, I'm so curious now whether it will work or not, I'll have to try it.
    {{ DiscussionBoard.errors[1413040].message }}
  • Profile picture of the author shaddai
    Plaster this code into that plugin & it echo's the current category name.

    <?php
    foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
    }
    ?>

    From there you can drop the echo line, then use standard php if/else statements to get to what you want it to do.
    {{ DiscussionBoard.errors[1413114].message }}
  • Profile picture of the author shaddai
    Roberts idea works too.. I just had if/else stuck in my head & looked for a quick way to get there.
    {{ DiscussionBoard.errors[1413145].message }}
  • Profile picture of the author webtrix
    You can do this very easily yourself, try this:

    Code:
    <?php if (is_category('Category A')) { ?>
    
    <p>Offers for category A</p>
    
    <?php } elseif (is_category('Category B')) { ?>
    
    <p>Offers for category B</p>
    <?php } else { ?>
    
    <p>This is some generic text to describe all other category pages, 
    I could be left blank</p>
    <?php } ?>
    Source: Category Templates WordPress Codex
    {{ DiscussionBoard.errors[1417508].message }}
  • Profile picture of the author HomeComputerGames
    Usually when dealing with this style you would use a switch/case style of programming:

    switch ($category_variable) {
    case "a":
    do something;
    break;

    case "b":
    do something;
    break;

    default;
    do something if noting matches or leave this out;
    break;

    }
    Signature

    yes, I am....

    {{ DiscussionBoard.errors[1423550].message }}

Trending Topics