I’m trying to add a custom field to a post title (sample page). To do so, I’m following the instructions given in this previous question, but the variable is not appearing in the title as expected. In fact, the exact same title appears. Any help would be appreciated.
The previous instructions indicate that the following code should be added to functions.php:
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;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });
The variable in my case is propertysq, not release_author. But a simple find/replace resulted in the following…
What occurs when I implement this code?
The current page title is: HS0525 - Chaweng Noi - Horizon Homes Koh Samui
This title has been inserted automatically by a WordPress plugin “Yoast SEO.” But after I disable this automatic title insertion, and insert the above code, the title inserted onto the page is identical to the previous page title: HS0525 - Chaweng Noi - Horizon Homes Koh Samui
Possible Sources of Error
Possible Source of Error #1: The aforementioned plugin/mechanism that is forcing its own <title>
onto the page. I am currently researching if this is the cause. I’d like to avoid completely removing the plugin, but I may have to.
Possible Source of Error #2: Would I need to edit the following line to correspond to my theme/functions?
$release_author = get_post_meta(get_the_ID(), "release_author", true);
I tried replacing it with the following line, but it did not work.
$propertysq = ale_get_meta('propertysq');
I should note:
- This site does not have a proper test environment setup where I can easily see PHP errors and dump vars.
- Per the previous answer, I also remembered to comment out the
<title>
tag in my header.php.
edit: Here is the exact code I’ve inserted into functions.php:
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
$propertysq = get_post_meta(get_the_ID(), "propertysq", true);
if($propertysq != "")
{ $title["title"].= " (" . $propertysq. ")";}
}
return $title;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });