Creating a Unique Homepage using Conditional Tags in WordPress

November 26, 2007

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.

How to Widgetize Your WordPress Footer

November 16, 2007

Widgets are great for people using WordPress who don’t want the hassle of hard-coding their sidebar. Many themes have a widgetized sidebar or even two. But what about widgetizing your WordPress footer? I looked for a tutorial, but couldn’t find one, so I’m presenting my exploration in widgetizing my WordPress footer for future posterity.

First, open the function.php file found in your template folder. Search for the sidebar function. It should look like this:

if ( function_exists(’register_sidebar’) )
register_sidebar();

If it’s not there, add it. If you want to have multiple widget sidebars, then you need to alter the code a little bit. For example, if you wanted a widgetized sidebar and a widgetized footer, we would add this code instead:

if ( function_exists(’register_sidebars’) )
register_sidebars(2);

You could change the number 2 to reflect the number of different widgetized sidebars you want to use.

Calling the Function

To call this function when we have just one dynamic sidebar, we would use the following code in our sidebar.php file or footer.php file, depending on where you want the sidebar:

<?php if ( !function_exists(’dynamic_sidebar’) || !dynamic_sidebar() ) : ?>

However, if we have multiple sidebars, we’ll use this code for the first sidebar:

<?php if ( function_exists(’dynamic_sidebar’) && dynamic_sidebar(1) ) : else : ?>

And this code for the second sidebar, to be placed in the footer.php file if you want a widgetized footer:

<?php if ( function_exists(’dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>

To end the dynamic sidebar, add this code:

<?php endif; ?>