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?


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.

| improve this answer | |

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.