Showing the current year in WordPress posts
Ce contenu n’est pas encore disponible dans votre langue.
Sometimes you may want to automatically display the current year in your WordPress posts or pages—for example, in a copyright notice or footer. Instead of manually updating it every year, you can use a shortcode to keep it dynamic. On ruachost.com, this can be achieved by adding a simple PHP function.
Why Use a Dynamic Year?
Section titled “Why Use a Dynamic Year?”- Keeps your site up‑to‑date automatically.
- Saves time by avoiding manual edits each year.
- Useful for copyright notices, seasonal content, or date references.
Steps to Add Current Year Shortcode
Section titled “Steps to Add Current Year Shortcode”-
Log in to WordPress with an administrator account.
-
In the dashboard, go to Appearance → Theme Editor.
-
From the right‑hand column, select the file named functions.php.
-
Scroll to the bottom of the file and add the following code:
function current_year_shortcode() {$year = date('Y');return $year;}add_shortcode('year', 'current_year_shortcode'); -
Click Update File to save changes.
⚙️ Using the Shortcode
Section titled “⚙️ Using the Shortcode”-
To display the current year anywhere in a post or page, type:
[year]
✅ WordPress will automatically replace [year] with the current year (e.g., 2025).
Important Notes
Section titled “Important Notes”- Always back up your site before editing theme files.
- Consider using a child theme to preserve changes during updates.
- This shortcode works in posts, pages, and even widgets that support shortcodes.
Cette page vous a-t-elle aidé ?
Merci pour votre retour.