Creating a dynamic page or post title

Image Courtesy Michelle Chance-Sangthon
If you’re not a fan of using a WordPress SEO plugin to manage your page title’s etc, then there’s a very good way to do this yourself by using WordPress Conditional Tags.
Essentially what you’re doing is adding an if/else statement that caters for your different template types. Your standard title tag might look something like this:
<? bloginfo('name'); ?> | <? bloginfo('description'); ?>
This would make the title of your webpage be the title of your site, followed by a pipe “|” and then the description of your site (as entered in your WordPress settings). However the problem with this is that your other pages are then not optimized using that page or posts title. What you want to do then, is make conditional statements that give a unique title on each page. This is the code you’re going to use:
<title>
<? if ( is_home() ) { ?><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?><?php } ?>
<? if ( is_search() ) { ?>Search Results | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_author() ) { global $wp_query; $curauth = $wp_query->get_queried_object(); ?>Author Archives | <?php echo $curauth->nickname; ?> | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_single() ) { ?><?php wp_title(''); ?> | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_page() ) { ?><?php wp_title(''); ?> | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_category() ) { ?><?php single_cat_title(); ?> | Archive | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_year() ) { ?><?php the_time('Y'); ?> | Archive | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_month() ) { ?><?php the_time('F Y'); ?> | Archive | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_day() ) { ?><?php the_time('d F Y'); ?> | Archive | <?php bloginfo('name'); ?><?php } ?>
<? if ( is_404() ) { ?>Page not found...<?php } ?>
<? if (function_exists('is_tag')) { if (is_tag()) { ?><?php single_tag_title("", true); ?> | Tag Archive | <?php bloginfo('name'); ?><?php } } ?>
</title>
What you have now is a page title that is setup to display a unique title for each of your main WordPress templates.
Have you tried a different method? Let us know in the comments.



Share This Post!