Menu Close

Show Something Only On The WordPress Homepage

default

Sometimes you have a piece of code, text or an advertisement you wish to show on your site’s homepage (and is not part of home.php) but you do not want to show it on all the other pages of your site. Below are two different ways you can do this. Both are really easy and even the biggest WP noob should be able to do it.

Checking If Is_Front_Page

In my opinion this is the best method to use. Basically you run a very quick php test to see if it’s the current page is front page of your site and if so show whatever. See the example code below:

<?php if ( is_front_page() ) { ?>
	Do stuff here
<?php } ?>

Checking If Is_Home

Alternatively you can check to see if the current page is the “homepage” and if so do something. See the example below:

<?php if ( is_home() ) { ?>
	Do stuff here
<?php } ?>

If Is_Home VS. Is_Front_Page

You are probably wondering what the difference between if “front_page” and “home“. It’s really simple. Basically the “front page” is the very first page of your site (the homepage) while “home” is the page where your latest posts is shown.

Use is_front_page whenever you want something to appear on the homepage no matter if it’s a static page or your latest posts.

Use is_home when you want to show something on the main blog page even if it’s not set as the homepage.

The Alternative Homepage-Only Sidebar, Header or Footer

Another options is to create a homepage-specific sidebar, header or footer that contains the code, text or advertisement that you wish to show only on the homepage. To do this just follow the simple steps below:

  1. Create new homepage specific file (sidebar-home.php, footer-home.php or header-home.php)
  2. Add whatever you need to your file
  3. Call new file on the home.php or index.php file (whichever your template is using for the homepage) – to do so simply use one of the methods below:

<?php get_header( home ); ?>

<?php get_sidebar( home ); ?>

<?php get_footer( home ); ?>

This method is a bit more of a pain in the a** because you have to create new files. However, it works well and if you have a completely different sidebar, footer, header for the homepage it may be a good way to keep things organized in your theme’s files.

Questions? Comments?

If you have any questions or comments regarding the methods for conditionally showing content only on your WordPress homepage let us know in the comments so we can help you out. I’m sure many other people have the same questions and you’ll be doing everyone a service!

View Source
Posted in WordPress