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…
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.