4

Certain posts of mine will have the custom field release_author in them - others don't. If that field exists, I want to display that in the header.php's title (not in the h1 of the post itself.

Right now this is the PHP that displays my title from the header.php file:

<title><?php wp_title( '|', true, 'right' ); ?></title>

So right now this is what it would display:

Random Title Name

This is what I want it to display:

Random Title Name (Author Name)

Does anyone know how to do this? I found a ton of tutorials similar to it, but they are all for modifying the title on the page itself, not for altering what shows up in the header...

  • Do you use add_theme_support('title-tag') or hardcode it? – MinhTri Apr 21 '16 at 3:46
  • @Dan It's hardcoded at the moment - the title tag is the code snippet above :) I've added an answer to convert it to the new way of doing things, though. – Tim Malone Apr 21 '16 at 4:01
  • How did you go with this, user92726? – Tim Malone Jul 6 '16 at 4:00
3

What you're looking for are the filters that let you modify the page title. There is a new way of doing this since Wordpress 4.4, so I'm going to focus on that. If you happen to be using a version prior to 4.4 and you can't update, the alternative is the wp_title filter (not to be confused with the wp_title() function), which I can elaborate on if you'd like.

So, the filter you are after (for WP 4.4+) is called document_title_parts.

To use this, you want to open your theme's functions.php file and place in the following:

add_filter("document_title_parts", "wpse_224340_document_title");

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular()){ // check we're on a single post
        $release_author = get_post_meta(get_the_ID(), "release_author", true);
        if($release_author != ""){ $title["title"].= " (" . $release_author. ")"; }
    }
    return $title;
}

You'll also need to remove that <title></title> tag that you found in your header.php, and add the following code to your functions.php as well:

add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });

This is telling Wordpress that it's safe to add its own <title> tag, which you're then modifying with the document_title_parts filter above.

EDIT: As this is a fairly new change to Wordpress, if you're interested in the background behind it or you want to know why it's best not to use wp_title anymore, you can see this blog post.

  • 2
    It will add blank () to every title. You should also check is_singular() as well. – Sumit Apr 21 '16 at 7:29
  • 1
    Thanks for pointing that out @Sumit - I've edited the post :) Feel free to add any other enhancements you have. – Tim Malone Apr 22 '16 at 2:47
  • Haha oops, thx for that syntax error fix too @Sumit! – Tim Malone Apr 22 '16 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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