Support » Fixing WordPress » PHP-coding help: how to exclude and include divs

  • Resolved dekraan

    (@dekraan)


    Hi there!

    I have one div, that I want to appear on my homepage, and another div that should only appear on the other pages in the same location.

    I’ve got the first part right with the following code, but I don’t know how to get the other div when I am not on home:

    `
    <?php if (is_home()) echo ‘<div id=”whitepaper”><h2 class=”description”>text</h2></div>’; ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Look at using an “else” statement along with your “if
    http://www.w3schools.com/php/php_if_else.asp

    Hi Andrew,

    thank you for the link. I don’t really know that much of what I am doing, and I noticed the examples there have the echo part between { and }. In my (working) code, it is not. How should I arrange this?

    Just by looking at w3schools I thought something like:

    <?php
    if (is_home())
    echo '<div id="home">text</div>';
    else
    {
    echo '<div id="other">text></div>';
    }
    ?>

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    How should I arrange this?

    To make the code easier to read you should use those curly braces to wrap code inside “if” and “else” statements.

    For example,

    <?php
    if ( is_home() ) {
     echo '<div id="home">text</div>';
    }
    else {
     echo '<div id="other">text></div>';
    }
    ?

    Does that help?

    Hi Andrew,

    yes it does, very much. I thought at first that it made my site disappear, but that is because you missed a > at the end. Now it works great! And I’ve learnt something about those braces and if and else. Couldn’t be better! A sunday well spent, even if this is the only thing I do 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PHP-coding help: how to exclude and include divs’ is closed to new replies.