Don’t you hate default stuff? By default, WordPress page TITLE (wp_title()) output might look pretty ugly and incomplete. Even worst if the TITLE is missing on some pages. Usually, there are few pages that have not been titled properly like the Search Result page, Paged page, Tag Archive page, Attachment page and Author Archive page.
This might not be the best code but it should complete the missing puzzle. Modify the code to suit your blog setting or language.
TITLE code
Open your template header.php and replace the entire TITLE tag (<title>…</title>) with:
<title>
<?php if ( is_paged() ) : ?><?php $paged = '— Page '.$paged; ?><?php endif; ?>
<?php if ( is_404() ) : ?>Page Not Found — <?php bloginfo('name'); ?>
<?php elseif ( is_home() && !is_paged() ) : ?><?php bloginfo('name'); ?>
<?php elseif ( is_search() ) : ?>Search Result for <?php echo wp_specialchars(stripslashes($_GET['s']), true); ?> <?php echo $paged; ?>
<?php elseif ( is_attachment() ) : ?>You are viewing Attachment <?php echo trim(wp_title('—', false)); ?>
<?php elseif ( is_single() ) : ?><?php echo trim(wp_title('', false)); ?>
<?php elseif ( is_category() ) : ?>You are browsing <?php echo trim(wp_title('', false)); ?> category <?php echo $paged; ?>
<?php elseif ( is_author() ) : ?>You are browsing Author Archive for <?php echo trim(wp_title('', false)); ?> <?php echo $paged; ?>
<?php elseif ( is_archive() ) : ?>You are browsing Archive for <?php echo trim(wp_title('', false)); ?> <?php echo $paged; ?>
<?php elseif ( is_paged() ) : ?><?php bloginfo('name'); ?> <?php echo $paged; ?>
<?php else : ?>
<?php echo trim(wp_title('', false)); ?> — <?php bloginfo('name'); ?>
<?php endif; ?>
</title>
If you use UTW plugin, you might want to add this code into the code above:
<?php elseif ( function_exists('is_tag') and is_tag() ) : ?>You are browsing Tag Archive for <?php echo $tag; ?> <?php echo $paged; ?>
Please don’t ask me why I use endif.
Not a regular dash
I’m using the em dash (—), also known as the em rule rather than the usual – (–). It is twice as wide as the en dash. The HTML entity for em dash is —.
If I’m missing something, feel free to notify me.
[...] Better TITLE for your WordPress wordpress title hack (tags: wordpress tutorial title wp_title) [...]
It’s typographically wrong to use an em dash for the Wordpress titles. Is there any reason why you did so?
Hin Ching, I’m breaking the rules because sick and tired of normal and obvious use of dash.
I’ve written some instructions for changing the behavior of wp_title to produce better page titles. This includes a method for removing the separator and also for removing unwanted spaces.
http://www.ardamis.com/2006/07/03/optimizing-the-s...
What about paged articles? The function
is_paged()doesn’t apply to those articles which use the <!–nextpage–> tag, and the article uses the same title tag for each page. How can I overcome this?Great code, by the way. I’ve been searching for this almost as long as I’ve been using WordPress!
Jonathan, just add
<?php echo $paged; ?>tois_single()line and any other lines that fit.Kool. I’ll give that a try. Thanks again!
Ah. I thought I recognized that call. The problem seems to be that WP doesn’t think a paged article is the same as a paged index. See here about half-way down: Conditional Tags.
Oh! Didn’t realized that. I can’t help you then. Sorry.
Ah. Oh well. You’ve already helped tremendously.