Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Current Year in Wordpress

Tags:

php

wordpress

I need to display the current year in Wordpress, now this is already being done in the footer template using php:

<?php echo date('Y'); ?>

The site in question is for a car dealer. So there are many year model references in the page/post content ie.

<h1>2013 Volkswagen Polo</h1>

All of these need updated on the first of January each year. However the php code doesn't work within the page/post content, only in template files. So I need a different way to do this.

Any ideas?

Thanks in advance
Willem

like image 271
Willem Avatar asked Dec 04 '13 08:12

Willem


People also ask

How do I add the current year to my footer in WordPress?

We used an add_shortcode function to create a WordPress shortcode and a custom function which returns the current year. function current_year_shortcode_function() { $year = date('Y'); return $year; } add_shortcode('year', 'current_year_shortcode_function');

How do shortcodes work in WordPress?

what are shortcodes in WordPress? In a nutshell, a shortcode is a small piece of code, indicated by brackets like [this] , that performs a dedicated function on your site. You can place it just about anywhere you'd like, and it will add a specific feature to your page, post, or other content.


2 Answers

Why don't you create the simple shortcode function for this purpose?

function currentYear( $atts ){
    return date('Y');
}
add_shortcode( 'year', 'currentYear' );

Then you will be able to put [year] to anywhere in the content area.

like image 130
Igor Evstratov Avatar answered Nov 15 '22 02:11

Igor Evstratov


A little modification of previous answer which also works fine:

in functions.php:

<?php
    function currentYear(){
        return date('Y');
    }
?>

then in footer.php (or anywhere as mentioned before):

<p>&copy; 2016-<?php echo currentYear(); ?> <a href="#">your_homepage_title</a></p>
like image 39
Tsugaru Avatar answered Nov 15 '22 04:11

Tsugaru