Nov
04

Using Conditional Tags for Page Titles

You can give different sections of your site a unique title so that your readers know where they are within your site. It’s a little thing - but its helpful to keep your visitors mindful of where they are at. For instance, if they are on a category page - it’s nice to have a title at the top that says something like “You are browsing the X category”… or when they click on one of your tags, give an announcement at the top of the page that says something like “Browsing posts tagged with: X” - that kind of thing.

With WordPress templates, you can create all sorts of different templates for different custom displays. However, to accomplish the title idea - you really only need one single template and a handful of conditional tags.

Create a template and name it: archive.php

That template should contain all the template tags necessary to display your posts and content the way you want them displayed.

To give that page a title, depending on what type of page is being displayed - you just need to add conditional tags at the top, and ABOVE the Loop1 . Here’s a sample of the conditional tags I use on my archive.php template:

<?php if ( is_day() ) : ?>
<h2 class="page-title"><?php printf(__('Daily Archives: <span>%s</span>'), get_the_time('F jS, Y')) ?></h2>

<?php elseif ( is_month() ) : ?>
<h2 class="page-title"><?php printf(__('Monthly Archives: <span>%s</span>'), get_the_time('F Y')) ?></h2>

<?php elseif ( is_year() ) : ?>
<h2 class="page-title"><?php printf(__('Yearly Archives: <span>%s</span>'), get_the_time('Y')) ?></h2>

<?php elseif ( isset($_GET['paged']) && !empty($_GET['paged']) ) : ?>
<h2 class="page-title"><?php _e('Blog Archives') ?></h2>

<?php elseif ( is_tag() ) : ?>
<h2 class="page-title"><?php single_tag_title('Posts Tagged With: '); ?></h2>

<?php endif; ?>

Take a quick look at the very last conditional tag in my code bits above:

<?php elseif ( is_tag() ) : ?>
<h2 class="page-title"><?php single_tag_title('Posts Tagged With: '); ?></h2>

The first line poses a question: Is this a tag page?

If the answer is yes.. then the title displayed at the top says “Posts Tagged With: TAGNAME”.

It’s a small gesture - but much appreciated by browsers of your site content.

  1. the loop starts with: if(have_posts(… []

2 Responses to “Using Conditional Tags for Page Titles”

  1. WordPress 2.3 - Tagging Feature — WP Assist Says:

    […] (OR.. you can include this in your archive.php template and use conditional tags to customize the display of titles on the pages of the different sections of your s…) […]

  2. Jonathon Says:

    TikiWiki has something like this: “BreadCrumbs” - a kind of “You Are Here” marker, based on the idea of leaving a trail of breadcrumbs if you are lost in the woods and wanting to be able to venture away but still come back to where you started. (Of course chipmunks eat breadcrumbs.)

    DOES WordPress have a BreadCrumbs feature or plugin?

Leave a Reply »

Close
E-mail It