I am new to WP so the issue is the following: I want to display the Month's name of the last updated date of my post in title tag. How can I do this?

New contributor
Christopher Coles is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 ways to do this.

Method 1. Modifies the title output everywhere. In your theme's functions.php file, put the following code:

function cc_month_in_title($title, $id) {
 $month = get_the_modified_date(' F, Y', $id);
 $title = $title.$month;
 return $title;
}
add_filter('the_title','cc_month_in_title', 10, 2);

Method 2. Modifies the title output only in the specific template. In this case, open the theme template file (say single.php) and look for the function the_title(). Replace it with the_title("", get_the_modified_date(' F, Y'));

In my example, it will output "November, 2018" but you can change that by modifying the date format output string in the get_the_modified_date().

Re-reading your question, I might need to amend this. Editing the title tag, not the title output is a little different. Lots of SEO plugins can do this easily. Yoast SEO handles it in its settings (free version, too) so you may want to look at that.

If you don't want to use the plugin (strongly recommended since the title tag is important for search engines), you can hook into the document_title_parts filter in your theme's functions.php file. If you want to do this for a particular template, you need to put the add_filter function below in the template before get_header() is called.

function cc_month_in_title_tag($title) {
 global $post;
 $month = get_the_modified_date(' F, Y', $post->ID);
 $title['title'] = $title['title'].$month;
 return $title;
}
add_filter('document_title_parts', 'cc_month_in_title_tag', 15);

Changing your title tag is going to mess with search engines so if this is important to you, you might not want to go down this path.

Your Answer

Christopher Coles is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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