I have a wordpress site. I have a div that I only want to appear on the homepage (in the header). What type of php if statement do i need...

In other words:

only show .... in the header of the main blog index page. Problem is the header itself is loaded in every page.



See: http://codex.wordpress.org/Function_Reference/is_home

<?php if(is_home()): ?>

<div>Your div.</div>

<?php endif; ?>
  • thanks.. my problem is that my theme effectively sets the homepage to display a "category". Is there a similar statement like (is_category_XXXX)? – NKeating May 5 '12 at 2:14
  • @NKeating Did this not work? – iambriansreed May 5 '12 at 2:16
  • it hid the div from the homepage because it does not detect it as home.. In other words, the homepage is set to display a category index called videos. See URL: speedsociety.com – NKeating May 5 '12 at 2:19
  • 1
    Also try: is_front_page(). – iambriansreed May 5 '12 at 2:25
  • 2
    Also try: reading all of codex.wordpress.org/Function_Reference/ There's some handy stuff on there. – JakeParis May 5 '12 at 2:36
<?php if(is_home()): ?>

<div>Your div.</div>

<?php endif; ?>

Using above code works fine as long as you are not setting static Page for the Front Page display from here Administration > Settings > Reading.

<?php if(is_front_page()): ?>

<div>Your div.</div>

<?php endif; ?>

But this code will work irrespective of whether the main blog page is showing or you have set a static page to show on home page.


There's always $_SERVER['REQUEST_URI'] you can use to determine which page you're at. For homepage it is usually just '/'. so:

<?php if( $_SERVER['REQUEST_URI'] == '/' ) { ?>
<div id="home_only">Content goes here..</div>
<?php } ?>
  • a) What about index.php? and b) that type of solution won't really work with Wordpress. – JakeParis May 5 '12 at 2:37
  • a) I don't see a reason why anyone would let people to view their homepage like domain.com/index.php b) What makes you think it won't work? – Conrad Warhol May 5 '12 at 2:46
  • ...Does it work? – JakeParis May 5 '12 at 2:48
  • Yes it works like a charm. – Conrad Warhol May 5 '12 at 2:51

You could also solve the problem with CSS.

Set the div to

display: none;

in your css.

Then find the body class page-id for the front page and set the css for that as:

.body-class-page-id #your-div {
display: block;

This will hide it on every page except the page you want to show it on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.