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.

I want to do something very simple but I'm stuck finding where in WordPress I need to perform this.

When someone on my WordPress site visits a blog post page I want the title in the blog post to replaced the title of the page.

I think I can do this with the wp_title filter hook?

I thought about something like the following :-

add_filter('wp_title', 'filter_pagetitle');

function filter_pagetitle($title) {
 $the_post_id    = get_the_ID();
 $the_post_data  = get_post($the_post_id);
 $title = $the_post_data->post_title;

 return $title;
}

However I am a bit lost as to where I put this, I thought it would need to be in loop-single.php as I want this to apply only to single pages, but I have also seen that this needs to be in functions.php within my theme?

Any help would be appreciated :-)

Rich

share|improve this question
add comment

1 Answer

Since wp_title() is usually called from the header.php file of your theme, then it runs on every page of your WordPress (frontend usually). So place the filter hook and function in your theme's functions.php file, and just check if it's a blog post before you change the title. Something like this:

add_filter('wp_title', 'filter_pagetitle');
function filter_pagetitle($title) {
    //check if its a blog post
    if (!is_single())
        return $title;

    //if you get here then its a blog post so change the title
    global $wp_query;
    if (isset($wp_query->post->post_title)){
        return $wp_query->post->post_title;
    }

    //if wordpress can't find the title return the default
    return $title;
}
share|improve this answer
    
This is pretty much what I thought would work also, the issue I have is that functions.php seems to think that this isn't a single post, so just returns the $title var –  Richard Bagshaw May 8 '12 at 9:51
    
it has nothing to do with the functions.php file, its called when your header.php file is included and if it has wp_title() in it. –  Bainternet May 8 '12 at 12:55
3  
I agree with the approach, but why not just use the $post global directly? i.e. global $post; $title = $post->post_title; I don't think there's any need to call get_post() here. –  Chip Bennett May 8 '12 at 14:19
2  
Two other points: 1) the wp_title() template tag already returns the Post Title in the context of a single blog post or static page, so this filter is actually redundant. 2) you may consider concatenating the returned $title content, so as not to stomp on Plugins or other functions (SEO Plugins, etc.) filtering the document title. –  Chip Bennett May 8 '12 at 15:11
1  
I think you may have additional Filters already hooked to your Title. Try deactivating your Plugins and switch to Twenty Twelve, and see if the proble still exists. –  fischi Feb 19 '13 at 16:27
show 3 more comments

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.