How to display current year in WordPress posts? Automatically update title year shortcode

share one inWordPressTips for outputting the current year without relying onWordPress plugin, quickly and automatically update the year in the title, footer or article content via shortcode.

How to display current year in WordPress post title?

For example, you can easily add the latest copyright statement to the footer of your website, or update the year in the title of some review articles.

For example: write "马来西亚AlipayHow to perform real-name authentication?【year】Alipay verification tutorial"▼

How to display current year in WordPress posts? Automatically update title year shortcode

This method is simple and easy. Just add the following code to the functions.php file, and then use it where you need to update the year.【year】This shortcode will do the trick:

function currentYear( $atts ){
    return date('Y');
}
add_shortcode( 'year', 'currentYear' );
//在标题中使用短代码
add_filter( 'wp_title', 'do_shortcode', 10);
add_filter( 'the_title', 'do_shortcode', 10);
  • If usingcode snipetsplugin orwpcodeIf the plug-in adds this PHP code, it may not take effect on the article title (it can only take effect on the article content). You need to add the PHP code to the functions.php file to take effect on the article title.

Please Note

Please enclose the brackets when actually applying WordPress shortcodes【】To[],This articleExamples使用【】This is to avoid incorrect conversions.

This shortcode does not work withSEOTitle and meta description, because depending on the SEO plugin you use, there will usually be dedicated code to handle this part of the content.

How do Rankmath and Yoast SEO plugin title descriptions display the current year?

For example, in the two plugins Rankmath and Yoast, you can use variables%%currentmonth%%%%currentyear%%, showing the latest month and year on the search engine results page (SERP).

  • The article title and content display the current year shortcode:【year】
  • The title and description of the SEO plugin show the current year variable:%%currentyear%%

Deprecated Yoast SEO variables in WordPress

Starting with Yoast v7.7, Yoast has deprecated these variables ▼

variableDescription
%%userid%%Replaced with the user ID of the post/page author
%%currenttime%%Replace with current time
%%currentdate%%Replace with current date
%%currentday%%Replace with current date
%%currentmonth%%Replace with current month
%%currentyear%%Replace with current year
  • Because Yoast found they had no valid use cases.
  • If they are used in the snippet editor, they will not appear in the snippet preview.
  • However, they will appear in your source code to maintain backward compatibility, but Yoast recommends not using them.

Solution:

  • Because these variables cannot be displayed or edited in the Yoast search appearance preview, Yoast recommends not using them.
  • However, we can modify and edit Yoast’s SEO title and description in “Yoast” → “Tools” → “Batch Editor”.

However, there is no search box provided on the batch editor page, making it difficult to find the articles or pages that need to be edited.

So we need to add 2 dots in front of the title of the article or page:..

Then click "WP Page Title" above the batch editor to sort, and you can quickly find the article or page that needs to be edited ▼

Then click "WP Page Title" above the batch editor to sort, and you can quickly find the article or page that needs to be edited. Picture 3

  • ▲In the "New Yoast SEO Title" input box, enter the SEO title with variables, and then click "Save".
  • After we edit the Yoast SEO title, and then return to the article or page we just opened, please refresh this page first (this is to update the Yoast SEO title just edited, otherwise it will overwrite and return to the Yoast SEO title that has not been edited just now) .
  • After refreshing the page, you can delete the 2 dots you just added before the article or page title...The.

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How to display the current year in WordPress articles?" Automatically update title year shortcode", it will be helpful to you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-31298.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top