Creating a Unique Homepage using Conditional Tags in WordPress
November 26, 2007 · Print This Article
Conditional tags in WordPress are awesome! Conditional tags (also called if statements) allow you to create content that only displays on certain pages. For example, let’s say we wanted to display a navigation bar on all of our pages, except the homepage ( a true example of something I needed to do for a client). So, we can use conditional tags to do this, but there are a few nuances. Let’s explore…
Using the code from the link above, we might arrive at something like this to add to our header.php file:
<?php if (is_home()) {
}
else { include (TEMPLATEPATH . ‘/navbar.php’);
}
?>
In this example, the first if statement is saying if this is the homepage do nothing, and including the navbar.php file for all other pages. This works great if we haven’t customized our blog, but we run into a problem if we’ve switch our homepage from displaying our dynamic blog posts to displaying a static page (done in the WordPress admin area under ‘reading options’). If that’s the case, then the is_home statement is still referring to our blog page, even though it’s not displaying as the homepage. So what do we do? Try adding this to your header file:
<?php if (is_page(’home’)) {
} else { include (TEMPLATEPATH . ‘/navbar.php’);
}
?>
The is_page targets whatever page we’ve switch to be our homepage. Just replace ‘home’ with whatever your static homepage name is. Now for the navbar file, we would create a separate file, called navbar.php and upload it to our theme folder. The code in that file might look something like this:
<?php
/*
Template Name: navbar
*/
?>
<div id=”navbar”>
<ul>
<li><a href=”<?php echo get_settings(’home’); ?>”>Home</a></li>
<li><a href=”/about”>About</a></li>
<li><a href=”/blog”>Blog</a></li>
<li><a href=”/contact”>Contact</a></li>
</ul>
</div>
Of course, you’d replace the links to represent the navigation you want to display, and the div id to reflect the id you’re using, or you can edit your style sheet to define the div properties. Now, when you load your website, the navigation bar will not display on your homepage, but it will display on all other pages.
You could use this same technique to change any number of things on your site to create a unique homepage, or use multiple if statements to create unique content on pages other than the homepage. For example, you might set it up so your header has a different picture for each page. Or you might create a different footer depending on the page. The possibilities are limitless using conditional tags in WordPress.
If you need help, feel free to contact me.






Comments
Got something to say?