Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

... or does wp_title() already handle the various contexts in your blog?

This could clarify for me how I can achieve a reusable index.php file without having all the conditional statements inside it to handle the different title formats in the given context (page, single, posts, search, archive, date, etc...)

Also,

Could a specific post or page have a special custom formatting without breaking the rest of the use-cases? (If this needs to be in a separate question, please let me know - it's closely tied to this question so I figured I'd merge it in here for now).

share|improve this question

2 Answers 2

up vote 3 down vote accepted

The wp_title() template tag does some context-based output. From the Codex:

The title text depends on the query:

Single post or a Page

  • the title of the post (or Page)

Date-based archive

  • the date (e.g., "2006", "2006 - January")

Category

  • the name of the category

Author page

  • the public name of the user

If you need to add more-specific contextual output, you can use the wp_title filter hook to modify the output.

For example, if you want to prepend the output with the Site Name:

<?php
function mytheme_filter_wp_title( $title ) {
    // Get the Site Name
    $site_name = get_bloginfo( 'name' );
    // Prepend it to the default output
    $filtered_title = $site_name . $title;
    // Return the modified title
    return $filtered_title;
}
// Hook into 'wp_title'
add_filter( 'wp_title', 'mytheme_filter_wp_title' );
?>

Taking an example from the Codex page, say you want to append the site description to the title output when on the site front page:

<?php
function mytheme_filter_wp_title( $title ) {
    // Get the Site Name
    $site_name = get_bloginfo( 'name' );
    // Prepend name
    $filtered_title = $site_name . $title;
    // If site front page, append description
    if ( is_front_page() ) {
        // Get the Site Description
        $site_description = get_bloginfo( 'description' );
        // Append Site Description to title
        $filtered_title .= $site_description;
    }
    // Return the modified title
    return $filtered_title;
}
// Hook into 'wp_title'
add_filter( 'wp_title', 'mytheme_filter_wp_title' );
?>

Modify accordingly, to suit your needs.

In case it isn't obvious, these filter callbacks belong in functions.php.

EDIT

Missed this:

Could a specific post or page have a special custom formatting without breaking the rest of the use-cases?

It is entirely possible. In fact, that's how most SEO Plugins work.

Here's the thing about using the wp_title filter to control wp_title() output: you've built in the ability to play nicely with SEO Plugins and anything else that attempts to modify wp_title() content, with no additional code changes required.

share|improve this answer
    
Thanks Chip! I was just about to ask before I read the whole thing whether or not the filter-hooks goes into functions.php - You sir provide the greatest answers I've seen so far! :) –  bigp Nov 1 '11 at 15:58
    
Happy to help! :) –  Chip Bennett Nov 1 '11 at 15:59
    
Does adding filters override the wp_title() functionality? Or does it add to existing filters of it? –  bigp Nov 1 '11 at 15:59
    
"Does adding filters override the wp_title() functionality? Or does it add to existing filters of it?" - it depends on what you return. You can completely override it, or just append content. –  Chip Bennett Nov 1 '11 at 16:26
    
Missed the second part of your question; it's now been added as an edit to my answer. –  Chip Bennett Nov 1 '11 at 19:01

Correct me if I am wrong, but I do not think that there is a "clean" way of doing titles without using conditionals. As far as the per-page title goes, you could create a custom meta field and check for the presence of it when you output your title.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.