• It’s been so long since I haven’t coded in PHP and I’ve forgotten how to use IF statements. I’m creating a new WordPress theme and I’m working on my sidebar which has certain elements that have to be displayed of specific pages.

    What I want to do is load the complete sidebar with all the elements in one file, depending on the page the user is on (e.g. About, Reviews, Headlines, Contact) I want certain parts on my sidebar to be displayed. I was thinking of using some IF statements; what I need to know is how to use them, let’s say the user is browsing the About page (about.php) and I want to display Box 1 on my side, the pseudo code would be:

    if file = about.php
    Display Box 1
    End if

    Also, I’m planning on using .htaccess rewrite rules, so intead of seeing the about URL as https://www.mydomain.com/about.php it will be https://www.mydomain.com/about, by doing this would it affect the way PHP knows while file the user is currently looking at or does PHP know that the user is viewing about.php?

    Additionally, let’s say Box 2 was only to be displayed on the homepage, what IF statement could I use for this? I also want to add an IF statement for certain categories (E.g. Reviews). If the user is looking at a post in the reviews section how would I go on about for displaying Box 3?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • If statement in PHP:

    https://php.net/if

    PHP will know what page you’re looking at if you use rewrites, but you’d have to use file commands to read the filename etc. It would probably just be easier, and certainly more secure, to set a variable in the page itself, so $page = "about", then

    if ("about" == $page) {
    // execute whatever
    }

    WordPress has built in functions for determining what type of page you are looking at:

    if (is_home()) {
    // homepage
    } elseif (is_category('3') {
    // category with ID of 3
    }

    More about conditional tags here:

    https://codex.www.ads-software.com/Conditional_Tags

    I would tell you off about not searching the codex, but the site search function on www.ads-software.com is so shockingly poor, you are completely forgiven.

    Thread Starter subfusion

    (@subfusion)

    Thanks for the replies.

    Maerk: I should have thought of that as well… Great idea ??

    Thread Starter subfusion

    (@subfusion)

    Just one last question, if I want Box 1 to appear on let’s say two pages what the following work?

    <?php if ($page == “about”) && ($page == “headlines”) { ?>
    Box 1
    <? } ?>

    Basically, Box 1 can appear on either the About page or the Headlines page.

    You’re nearly there!

    <?php if ($page == “about” || $page == “headlines”) { ?>
    Box 1
    <? } ?>

    When you want to combine two expressions, they always appear within the same parentheses.

    Oh, and || means “or” but && means “and”.

    for example

    if ( $a == 1 || $b == 2 ) {
    // if either $a is 1 or $b is 2
    } elseif ( $a == 2 && $b == 5 ) {
    // only if $a is 2 AND $b is 5.
    }

    Thread Starter subfusion

    (@subfusion)

    Thanks for the help ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Sidebar Help’ is closed to new replies.