Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

WordPress- Only Display Element On Homepage

A lot of the files in WordPress are used as includes e.g Sidebar, header and footer. What if you want certain elements in those includes to appear just on the Homepage? I find myself needing that feature all the time.

Example

All you really need is one simple function. So for example, in your header you may have an image which you want to appear on the homepage:

BrightCherry Logo

The Code

1
2
3
4
5
6
7
<?
if (is_home() && ) {
?>
<img src="http://www.brightcherry.co.uk/images/logo.png" title="brightCherry Logo" alt="BrightCherry Logo" />
<?
}
?>

If you use the above code, you will still see the image when you paginate to the second page of the homepage. If you strictly want it on the homepage, and no other pagination pages, you should use the following code:

1
2
3
4
5
6
7
<?
if (is_home() && strpos($_SERVER['REQUEST_URI'], "/page/") === false) {
?>
<img src="http://www.brightcherry.co.uk/images/logo.png" title="brightCherry Logo" alt="BrightCherry Logo" />
<?
}
?>

Now the image will only appear on the homepage :)

20 Oct 2008 / 7 Comments / Wordpress / by Maruf

7 Comments

  1. Aaron
    02/03/2009
    1

    Thank you so much for this, you just made my day.

  2. Rob Cubbon
    12/03/2009
    2

    This made my day as well. I had a whole load of javascript in the header that was necessary for the homepage but not for any other and was throwing up “object required” errors in IE. I kind of “commented it out” with this trick which I couldn’t find in the codex. Thank you!!!

  3. demo7up
    10/11/2010
    3

    awesome

  4. 01/02/2011
    4

    That code will not limit your display to your homepage. You will still see it when you paginate to the second page.

  5. Maruf
    09/03/2011
    5

    Thanks for pointing that out Gadizmo.

    I’ve provided another snippet of code which overcomes that problem.

  6. 20/06/2011
    6

    thank you so much! i’ve been messing around with conditional functions with no luck until i found this. much appreciated!

  7. 12/01/2012
    7

    Thank you a lot! This “page” part was nowhere mentioned in wordpress, I used only the “is_home” and obviously it wasn’t working for pages :( Very annoying issue! Thank you for posting this, it fixed my problem too!

Leave a Reply

© 2012 BrightCherry :)