Show Something Only On The WordPress Homepage

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_Font_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 something cool <?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 something cool <?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.

  • Updated on:
  • Posted Under: Tutorials
AJ Clarke
Post Author: AJ Clarke

Hey. My name is AJ and I am the boss around here. I own and operate WPExplorer. A website dedicated to everything I love about WordPress.

Disclosure

Leave a Reply

Your email address will not be published. Required fields are marked *