How to show the current year in WordPress posts

This article describes how to show the current year in WordPress posts. By automatically generating the current year using a shortcode, you can ensure that your posts are always up-to-date.

Showing the current year

To show the current year in WordPress posts, follow these steps:

  1. Log in to WordPress as the administrator.
  2. On the Dashboard in the left sidebar, click Appearance, and then click Theme Editor:

    WordPress - Dashboard - Appearance - Theme Editor

  3. In the right sidebar, under Theme Files, click Theme Functions (functions.php).
  4. Copy the following code snippet and then paste it at the bottom of the functions.php file:

    function current_year() {
        $year = date('Y');
        return $year;
    }
    
    add_shortcode('year', 'current_year');
    
  5. Click Update File. WordPress saves the changes to the functions.php file.
  6. The current year functionality is now enabled, but you must update the post or posts where you want it to appear. To do this, open a post or page on the site, and then type [year] where you want to display the current year. For example:

    WordPress - Current year example - Editing

    Save your changes, and then view the post:

    WordPress - Current year example - Post

Get WordPress Hosting

Article Details

Other Articles in This Category

Show More

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

Usamos cookies con el fin de personalizar para usted nuestro sitio Web y analizar el uso de éste. Usted acepta esto haciendo clic en “Acepto” o al continuar usando el sitio Web. Hay más información sobre cookies en nuestra política de privacidad.