How to show an image or add on the front page of a WordPress site using PHP

I do a lot of WordPress websites and one thing that comes up quite a bit is the need to show an image or add or other element only on a specific page like the front page. In order to do this I usually edit the theme files using PHP code with an “if” statement something like this <?php if(is_home): ?> or <?php if (is_home()): ?> then I write the code and close it out with <?php endif; ?>

Normally this is all it takes! There are many ways to write it but for some reason it doesn’t always work and it’s not because the PHP is wrong, it might be because you have a page setup as the front page and aren’t using the default “blog list”. This was the case recently for me and I discovered that using “is_front_page” works vs. “is_home” so give this a try if you’re having trouble.

Now another tip here is for if you only want the image or ad to show up on the first page of your “front page” type. That is, what if you have lots of posts and are using the blog style front page where WordPress lists out entries. Usually it paginates out so that only the first 10 entries are shown then you hit “previous entries” to see older links to articles. In this case you can use a combination of “is_front” and “!is_paged” to tell WordPress only show this item on the front page but not other pages, that’s what the exclamation point means, it means “not this”.

Here are a few code examples to get you started:

  • <?php if(is_home() && !is_paged()) { echo ‘<p>hello world</p>’; }?>
    • Writes hello world in paragraph on the front page of WordPress site but not paginated front pages.
  • <?php if(is_home()): ?> </p>hello world</p><?php endif; ?>
    • Writes hello world in paragraph on the front page and paginated front pages of WordPress site
  • <?php if(is_front_page()): ?> </p>hello world</p><?php endif; ?>
    • Writes hello world in paragraph on the front page of WordPress site that is not using the default home setup, it has some other page designated as front page under the “reading > front page displays” options

Just below is an image of the “Front Page” settings in WordPress. If you’re using these like in the picture and they aren’t set to “your latest posts” then you need to use “is_front_page” instead of “is_home”. Good luck!

Here is some actual code that I use in one of my sites where I am not using the default WordPress front page settings. For more on conditional tags and tips like this check out the WordPress Codex, you can use other tags to only show stuff on single entries or just on pages or whatever you want.

<!– BEGIN HEADER –>
<?php if (is_front_page()): ?>
<img id=”header” src=”<?php bloginfo(‘url’); ?>/wp-content/uploads/logo.jpg” alt=”<?php bloginfo(‘name’); ?>” />
<?php endif; ?>
<!– END HEADER –>