Why won't 'if' 'else' work in footer.php? (7 posts)

  1. Flamini
    Posted 7 years ago #

    Morning all,

    I need to hide a div tag in my footer.php file and I am using this:

    <?php if (is_home()) { ?><!-- /no-div --><?php } else { ?></div> <!-- /wrap --> <?php } ?>

    But it does not work.

    Am I doing something wrong? Any suggestions?

    Many thanks

  2. Vaughan
    Posted 7 years ago #


    I tried your code as is in a few themes and it worked perfectly by inserting <!-- /no div --> on the home page and </div><!-- /wrap --> on any other page. Perhaps there is an issue with the specific footer theme file you're using and that this is somehow being placed after the closing </body> or </html> tags?

    I'd suggest trying your code in another theme you may have or in another file of your theme (such as header.php or sidebar.php) just to test.

    All the best.

  3. NC@WP
    Posted 7 years ago #

    How exactly does it "not work"? Do you get error messages? If so, what are they? And, just to rule out the obvious, does your main theme file have a get_footer() call? In other words, does your theme even try to have a footer?

    Additionally, you can always consolidate your code into a single block, which will make it much easier to read and debug:

    if (is_home()) {
      echo '<!-- /no-div -->';
    } else {
      echo '</div> <!-- /wrap --> ';
  4. Flamini
    Posted 7 years ago #

    I think I found out why it is not working as I am using a static page for my homepage.

    WordPress introduced a great feature in versions 2.1+: a static home page. Twas the feature that made WP a viable CMS. But unfortunately it broke the internal is_home() function.
    The internal function is_home() will return true when the homepage is requested. But WordPress doesn’t take the static homepage into account, so in effect when you have a static homepage, this function will never return true.

  5. moshu
    Posted 7 years ago #

    Read the Codex, instead of making theories here...

  6. BritneyJackson
    Posted 7 years ago #

  7. moshu
    Posted 7 years ago #


    That is NOT the real solution... that's an obsolete workaround!
    Can you read?

Topic Closed

This topic has been closed to new replies.

About this Topic