WordPress How To – Changing Content Easily

A very frequent question about WordPress is how to show something only on the main page or, conversely, show something only on subpages. It’s super easy to do, but it’s a simple way you can make WordPress blog feel more dynamic.

Let’s say, for example, that you want a square ad to show in the sidebar of your site when people are on the main page … however, you don’t want that ad to show when they are on sub-pages. Instead, let’s assume that on sub-pages you want a thin vertical ad to show right above your post content. So, essentially you need to control two different ads, in two different places on your page, and you need to be able to tell if someone is on a main page or a sub-page.

This is not a new thing, and it’s something that most people who create WordPress themes do regularly. However, if you are one of those people (like most out there) who just do a little tinkering here and there, then this tip might be of help.

Using the example, let me show you the quick and easy way to accomplish this.

Note: You’ll need to be in your theme editor in the WordPress Admin.

Issue #1: You want to show the small box ad in the sidebar on the main page only.


First locate your sidebar file in the theme files. Then find the spot in your sidebar where the ad will go. When you have found it, add the following (inserting whatever you want to include where I’m putting the image code):

<?php if ( is_home() ) { ?>
<p><img src="ad.gif" alt="Your ad." /></p>
<?php } ?>

That will make certain that it only shows up if it’s the home page. You can enter any HTML you want between those two PHP calls.

Now for the sub pages.

Issue #2: You want to show an ad at the top of a post on sub-pages only.


First locate your single pages file in the theme files. Then find the spot in the file where the ad will go. When you have found it, do just as before and add the following:

<?php if ( is_single() ) { ?>
<p><img src="long_ad.gif" alt="Your long ad." /></p>
<?php } ?>

Now that ad will only show up on sub-pages.

Pretty simple isn’t it? 🙂

(End Note: The WordPress codex will show you a different way, but I’ve found this code to be significantly less confusing for most people than using echoed text.)