WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Creating a Year shortcode (3 posts)

  1. elhashbrown
    Member
    Posted 3 years 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?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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');
  3. elhashbrown
    Member
    Posted 3 years ago #

    That's got it. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic