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
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');
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.
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.
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>© 2016-<?php echo currentYear(); ?> <a href="#">your_homepage_title</a></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With