4

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!

1

4 Answers 4

4

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

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

<div>Your div.</div>

<?php endif; ?>
8
  • 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
    Commented May 5, 2012 at 2:14
  • @NKeating Did this not work? Commented May 5, 2012 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
    Commented May 5, 2012 at 2:19
  • 1
    Also try: is_front_page(). Commented May 5, 2012 at 2:25
  • 2
    Also try: reading all of codex.wordpress.org/Function_Reference/ There's some handy stuff on there.
    – JakeParis
    Commented May 5, 2012 at 2:36
4
<?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.

1

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.

1
  • This actually ended up making more sense for me. Commented Feb 9, 2021 at 4:22
0

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 } ?>
4
  • a) What about index.php? and b) that type of solution won't really work with Wordpress.
    – JakeParis
    Commented May 5, 2012 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
    Commented May 5, 2012 at 2:46
  • ...Does it work?
    – JakeParis
    Commented May 5, 2012 at 2:48
  • Yes it works like a charm.
    – Conrad Warhol
    Commented May 5, 2012 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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