WordPress.org

Ready to get started?Download WordPress

Forums

funny switch in the sidebar (4 posts)

  1. Chris Demetriad
    Member
    Posted 2 years ago #

    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 :)

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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; } ?>
  3. Chris Demetriad
    Member
    Posted 2 years ago #

    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/":
    ...
  4. esmi
    Forum Moderator
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic