WordPress.org

Forums

[resolved] PHP-coding help: how to exclude and include divs (5 posts)

  1. dekraan
    Member
    Posted 1 year ago #

    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>'; ?>

  2. Andrew
    Forum moderator & snail smusher
    Posted 1 year ago #

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

  3. dekraan
    Member
    Posted 1 year ago #

    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>';
    }
    ?>
  4. Andrew
    Forum moderator & snail smusher
    Posted 1 year ago #

    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?

  5. dekraan
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic