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:

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


brightcherry.co.uk
Thank you so much for this, you just made my day.
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!!!
awesome
That code will not limit your display to your homepage. You will still see it when you paginate to the second page.
Thanks for pointing that out Gadizmo.
I’ve provided another snippet of code which overcomes that problem.
thank you so much! i’ve been messing around with conditional functions with no luck until i found this. much appreciated!
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!