Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Thanks!

share|improve this question

migrated from stackoverflow.com May 5 '12 at 8:15

This question came from our site for professional and enthusiast programmers.

    
possible duplicate of Show a different code on front page to other pages –  Chip Bennett May 5 '12 at 23:47

4 Answers 4

up vote 3 down vote accepted

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

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

<div>Your div.</div>

<?php endif; ?>
share|improve this answer
    
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.

share|improve this answer

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 } ?>
share|improve this answer
    
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

#your-div{
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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