wordpress if statement not responding
The website I am working on has a different footer on the home page to all the other pages on the site. However the footer does not appe开发者_如何转开发ar on any of the pages at all. here is the code I am using:
<?php if ( is_home() ) {
// This is a homepage
?>
<div id="footer"> This is home page</div>
<? } else { ?>
<div id="footer">This is not a homepage</div>
<?}?>
Is my syntax wrong? Why doesn't this code work?
Thank you all in advance.
2 things I can think of here:
- You are using a static front page. In this case you will need to check for is_front_page()
- Using
<?php
open tags
Consider revising the code to following:
<?php if ( is_home() || is_front_page()) { ?>
// This is a homepage
<div id="footer"> This is home page</div>
<?php } else { ?>
<div id="footer">This is not a homepage</div>
<?php } ?>
Unless you're using an old version of php, you might want to switch <? } else { ?>
to <?php } else { ?>
- note that I'm using a <?php
instead of just <?
.
As for the is_home()
, you might want to read up on the following link: http://codex.wordpress.org/Function_Reference/is_home
It seems as if is_home()
sometimes isn't the same thing as is_frontpage()
, which means that you in some cases need to use is_frontpage()
instead.
Good luck!
Using <?php
open tags is one of the "best practices" in WordPress coding. Also, consider using <?php get_footer('footer-variant') ?>
instead of conditional statements in your footer.php. After calling get_footer('my-footer')
, footer-my-footer.php
file will be used.
精彩评论