[Resolved] Shortcode for current date

Home Forums Support [Resolved] Shortcode for current date

Home Forums Support Shortcode for current date

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #631189
    david

    hello,

    how to get current date with shortcode?

    #631204
    Leo
    Staff
    Customer Support
    #631253
    david

    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

    #631707
    Tom
    Lead Developer
    Lead Developer

    You can just use the shortcode inside the Page Header.

    It should use whatever language you have set: https://codex.wordpress.org/Formatting_Date_and_Time

    #632018
    david

    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

    #632065
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #632912
    david

    thanks tom, it works perfectly!

    #633328
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.