Site logo

Archived topic

How can I make year dynamic in content?

3 replies · Started by Former User on April 28, 2021

Viewing posts 1–4 of 4

Hello,

I notice that on footer on copyright module when you place %current_year% it shows you current year dynamically on copyright section. I was wondering how I can do the same in content, in title content and on metatags?

Thanks!

Sara

Hi Sara,

You can use this PHP snippet to create a shortcode for the year:

function year_shortcode() {
  $year = date('Y');
  return $year;
}
add_shortcode('year', 'year_shortcode');

Then use [year]in content.

Adding PHP: https://docs.generatepress.com/article/adding-php/

Hello Ying,

Thanks for the reply but, there a few problems with your solution that I mention bellow and you can see on my site URL bellow:

1. Does not work on metatags

2. Does not work on post title

Is there any other way to achieve this?

Hi there,

Shortcodes won't run from within fields such as the Content Title. That would require filtering the_title() or the particular post meta. It can be done with some code - but you would also need a mechanism that determines which post / terms should be filtered and which are not.

You can see an example here to add Year to the post title:

https://generatepress.com/forums/topic/post-year-next-to-post-title/#post-1063146

But as you can see from the OPs replies that it may require more conditions to target the right posts.

This archived topic is closed to new replies.