Support » How-To and Troubleshooting » Why won’t ‘if’ ‘else’ work in footer.php?

Why won’t ‘if’ ‘else’ work in footer.php?

  • 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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Vaughan



    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.

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

    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.

    Read the Codex, instead of making theories here…


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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Why won’t ‘if’ ‘else’ work in footer.php?’ is closed to new replies.