• So ive got 2 sidebars on the main page on this site (www.sikcyde.com), “sidebar.php”, and “sidebar2.php”. I have created additional sidebars for every other page I’ve created on the site. I want it so when you go to the page called “Mr. Liqz” it will load “liqzside.php” instead of “sidebar.php”(Which is the sidebar on the right side). So far with the switch I have this:

    <?php
    $page_title="sikcyde";
    switch ($page_title)
    {
    case "Mr. Liqz":
       include("liqzside.php");
       break;
    case "Nige":
       include("nigeside.php");
       break;
    case "Big Shot":
       include("bigshotside.php");
       break;
    default:
       include("sidebar.php");
    }
    ?>

    The only part of the code that works is:

    default:
       include("sidebar.php");

    If I change “sidebar.php” to “liqzside.php” it will load liqzside.php for EVERY page I have, and the same goes if I replace it with nigeside.php and all other sidebars I have. I know I messed up somehow, but I don’t completely understand where I went wrong as I followed instructions from W3’s website about switches. Only thing I think would be wrong would be the case line, but I’m not sure how to fix that

    Any tips or ideas on how to fix that?

    Thanks for any suggestions ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    Can anyone help with this issue??

    Just checking but…

    You didn’t leave $page_title="sikcyde"; in your code when you’re uisng it on your live site, did you? That would over-write the page title, and force everything to the default option.

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    I just tried:

    <?php
    
    switch ($page_title)
    {
    case "Mr. Liqz":
       include("liqzside.php");
       break;
    case "Nige":
       include("nigeside.php");
       break;
    case "Big Shot":
       include("bigshotside.php");
       break;
    default:
       include("sidebar.php");
    }
    ?>

    And that didn’t work, how exactly should it be to stop that?

    Where are you getting $page_title from?

    If you don’t know, try adding this:

    echo "<p>Page title: '".$page_title."'</p>";

    That will tell you what the page title is, or at least what your code thinks that it is. Knowing what that value is will tell you what the switch() is supposed to do.

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    Well I’m trying to do is have the script find the page name, then according to the page name its going to load the correct sidebar for that page. I got $page_title on a wordpress support page.

    When i ran :

    <?php
    echo "<p>Page title: ".$page_title."</p>";
    switch ($page_title)
    {
    case "Mr. Liqz":
       include("liqzside.php");
       break;
    case "Nige":
       include("nigeside.php");
       break;
    case "Big Shot":
       include("bigshotside.php");
       break;
    default:
       include("sidebar.php");
    }
    ?>

    All it did was display:

    page title:”

    Nothing else was displayed…. So what next?

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    I also tried: echotl “<p>Page title: $post_title </p>”; Still didn’t get anything to generate after “Page Title:”

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I think we need to see the entire code, can you post that on PasteBin and link is that?

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    that actually is the entire code lol. am I missing something?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Have you tried setting the variable $post_title with the get_the_title function?
    E.g

    <?php
    $page_title = get_the_title();
    
    echo "<p>Page title: ".$page_title."</p>";
    switch ($page_title)
    {
    case "Mr. Liqz":
       include("liqzside.php");
       break;
    case "Nige":
       include("nigeside.php");
       break;
    case "Big Shot":
       include("bigshotside.php");
       break;
    default:
       include("sidebar.php");
    }
    ?>

    https://codex.www.ads-software.com/Function_Reference/get_the_title

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    I have not tried it that way, but I did try using $get_the_title, but I know I used it wrong

    There’s no $ in front of function names, so that’s probably where you’re getting it wrong. What happens when you remove the $ and have it exactly as Andrew has said above?

    Thread Starter C0MPT3CHS

    (@c0mpt3chs)

    It worked ?? ?? ??

    And i left it as:

    $page_title = get_the_title();

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘PHP switch in wordpress?’ is closed to new replies.