• Resolved cedialogs

    (@cedialogs)


    I have defined several shortcodes in functions.php, all of which work perfectly. I want a shortcode for the current post’s date published (post_date) and have found many sample code snippets for functions that claim to return post_date, but every snippet returns either nothing or DateTime.Now. Here are some of my attempts at making a function that will retrieve post_date:

    function date_created( ) {
    return ‘Posted: ‘ . $post->post_date;
    /* return ‘Posted: ‘ . mysql2date(‘M d, Y G:i’, get_post($post_ID)->post_date); */
    /* return ‘Posted: ‘ . mysql2date(‘M d, Y G:i’, $post->post_date); */
    }
    add_shortcode( ‘created’, ‘date_created’);

    I am presuming that the variable $post_ID is previously defined and is not null. If it is undefined, I have no idea how to properly define it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    try to add:

    global $post;

    as the first line of the shortcode.

    Thread Starter cedialogs

    (@cedialogs)

    Thanks, Michael. I was given an alternative approach that is rather klunky but does seem to work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘shortcode for a post's date published.’ is closed to new replies.