Site logo

Archived topic

Shortcode for current date

7 replies · Started by david on July 23, 2018

Viewing posts 1–8 of 8

hello,

how to get current date with shortcode?

hello, your code works. i need a little further help.

my default language on wordpress is indonesian. how to make the shortcode works like {{post_date}} on page header? the page header use wordpress language i set. the shortcode you provide use english as date

no tom, i'd like to use on post. but it's output is english. i already use {{post_date}} on page header

i'd give you a visual

i'd like to get juli 2018 as result instead of july 2018

Try this for your shortcode:

add_shortcode( 'custom_date', function() {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string .= '<time class="updated" datetime="%3$s" itemprop="dateModified" style="display:none;">%4$s</time>';
    }

    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        $time_string
    );

    return $date;
} );

thanks tom, it works perfectly!

You're welcome :)

This archived topic is closed to new replies.