Switch and function

Discussion in 'PHP' started by Coupons, Feb 5, 2006.

  1. #1
    Greetings,

    I would like to echo some lines of text depending on the category name on Wordpress, but it could be at any other php script.

    single_cat_title() is a function that returns the name of the current category.
    If I make
    it will print house, for example.

    What I would like is to output the following line in the footer: "You can find houses at...", if category name is house.

    It could be used as small context related ads or quotes. But I tried many things and nothing worked. Even if I pass the result of the function to a variable, before using it in the switch command, it still isn't working. I must be doing something wrong.

    Anyone thought about it? It would be a nice way of having semi-contextual ads.
     
    Coupons, Feb 5, 2006 IP
  2. blinxdk

    blinxdk Peon

    Messages:
    660
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Perhaps something extra is getting outputted so that it'll never match? (line break or such).

    Try and copy only the first 5 positions of the output when it is "house" to a variable and switch on that, just to make sure.
     
    blinxdk, Feb 5, 2006 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,828
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #3
    I'd put in a temporary debug there
     <?php var_debug(single_cat_title()); ?>
    Code (markup):
    and see what's being spat out.
     
    sarahk, Feb 6, 2006 IP
  4. Coupons

    Coupons Active Member

    Messages:
    889
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Thank you for your help. I will try both tips. It's really printing just the word house, so it may be any invisible element like blinxdk said. Thank you again :)
     
    Coupons, Feb 6, 2006 IP
  5. Coupons

    Coupons Active Member

    Messages:
    889
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    70
    #5
    We should never do coding at high hours of the night... I just noticed that <?php echo single_cat_title(); ?> isn't outputting anything outside the category archive, so it's obvious this could never work on a single page as I was testing... Well, it was worth the try.

    I've read lots of topics:
    Page templates http://codex.wordpress.org/Pages#Page_Templates
    Category templates http://codex.wordpress.org/Category_Templates
    The Loop http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/Template_Tags/category_description
    http://codex.wordpress.org/Template_Tags/get_the_category

    But I'm not seeing how I can do this to work on single pages. It would have to call the category name correctly, and then output the correct sentence.
    This is easy to do in categories archives, but a little more difficult in single pages. I just start messing with Wordpress, and I still don't know the best functions to call.
     
    Coupons, Feb 6, 2006 IP