elhashbrown
Member
Posted 1 year ago #
I am trying to create a shortcode called 'year' that I can place within pages as I have documents which should always refer to the current year. I have used the following in my functions.php:
function year_shortcode() {
$year = the_time('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
This brings the current year into the page but it sits at the top of the div rather than inline with the text. I'm wondering if the_time is being echoed despite the fact I used return? Can I use &echo=0 somewhere?
this is because the_time() directly outputs the time;
use get_the_time() instead: http://codex.wordpress.org/Function_Reference/get_the_time
both of these wordpress functions refer to the published time of the post.
for the current time, try http://php.net/manual/en/function.date.php
this might do what you want; example:
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
elhashbrown
Member
Posted 1 year ago #
That's got it. Thank you.