WordPress.org

Support

Support » Plugins and Hacks » funny switch in the sidebar

funny switch in the sidebar

  • Can anyone give me a hand, please?

    I have this in my right sidebar, quite simple:

    <? $why = is_page();
    switch($why) {
    
    case "about-us":
    echo "About us";
    break;
    case "latest-news":
    echo "Latest News";
    break;
    case "our-community":
    echo "Our community";
    break;
    default:
    print "DEFAULT";
    break; } ?>

    Then the pages with that slug are calling or including (neither does work) the same side bar and I would like to display different content on each page.

    The switch does work for the about page but not for the other two and I think I am going crazy :). I mean, if you click on the about page it shows what it’s in the echo for that case, but Default for the others.

    is_page is supposed to get the slug, right?!

    Another curious thing, if I get the about-us case on the second position in the switch, after the latest news, when I click on about-us page I have the echo from latest-news and the rest just on Default! WHY?!

    Thank you 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    @esmi

    Forum Moderator

    is_page is supposed to get the slug, right?!

    Nope. It tests for a specific page using the page title, id or slug. The way that you are currently using it, it just tests to see if you’re on a Page as opposed to a Post. Try:

    <?php
    switch(true) {
    case is_page('about-us'):
    echo "About us";
    break;
    case is_page('latest-news'):
    echo "Latest News";
    break;
    case is_page('our-community'):
    echo "Our community";
    break;
    default:
    print "DEFAULT";
    break; } ?>

    I got it now, thank you so much.

    I was able to find another solution and I am not sure what to use now.

    ...
    switch ($_SERVER['REQUEST_URI']) {
    case "/about-us/":
    ...
    esmi

    @esmi

    Forum Moderator

    I’d be wary of using $_SERVER[‘REQUEST_URI’] on a WordPress site. That may not work in some situations.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘funny switch in the sidebar’ is closed to new replies.