Hello,
I’d like to modify the HTML title-tag on some posts based on some custom fields. I tried to ad the filter document_title_parts in my child-theme’s functions.php, but it seems, that this filter has no effect on the html-title. How can I modify the title-tag? See https://developer.wordpress.org/reference/hooks/document_title_parts/
Hi,
If you want to change the html post title you can try to us a single post template from tagDiv CLoud Library, edit it using tagDiv Composer and change the title, after that set the single post template on the posts that you want to have the title change.
-> https://www.screencast.com/t/hVTrqqBIW
If this is not what you mean, please provide some more details about what you want to achieve.
Thank you!
Hi Calin,
ideally I don’t want to change template-files to make them update-save. So I thought I can manipulate the titles with a function in functions.php. I tried something like:
function custom_title($title_parts) {
if (in_category('categoryname') && is_single()) {
$title_parts['title'] = some code here ...
}
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_title' );
But this seems to have no effect on the title.
Hi,
You can try to make the function more simple and test to see if this is working in child theme ( also make sure that the child theme is working properly).
You can check some more articles for this here:
-> https://firstsiteguide.com/wordpress-titles/
-> https://wordpress.stackexchange.com/questions/269862/how-to-set-custom-title-of-custom-page-template
-> https://orbitingweb.com/blog/custom-seo-friendly-title-tags-wordpress/
-> https://stackoverflow.com/questions/27373238/function-to-change-page-title-in-wordpress
Thank you!
Thank you, Calin!
I now was able to change the title. I used the filter
add_filter( 'pre_get_document_title', 'custom_title_function' , 50);
but it still didn’t work. The reason was Yoast SEO, which has a stronger impact on the title, but it has it’s own filter, so I additionally used:
add_filter('wpseo_title', 'custom_title_function');
This finally worked!