• Resolved Grant-Senior

    (@grant-senior)


    Hello, I’ve got the code below working ok (I need to extract the date the post was posted to go inside the content of the post with a shortcode), but it’s displaying the clock time as well eg, 2014-02-01 01:24:27 I would like it to display it like, Feb 1 2014. Can anybody tell me how I get this please? This is the shortcode that goes in the Post [mm-insert-date]

    Cheers

    //shortcode function in Theme functions.php

    function mm_insert_date_func($atts) {
    // Insert the post date, with optional before and after strings.
    // [mm-insert-date before='<h3>’ after='</h3>’]
    global $post;
    extract(shortcode_atts(array(‘before’ => ”, ‘after’ => ” ), $atts));

    $output = “{$before}{$post->post_date}{$after}”;

    return $output;
    }
    add_shortcode(‘mm-insert-date’, ‘mm_insert_date_func’);

    Here’s a link to where you’ll see it in action
    https://www.thewebsitedeveloper.co.nz/tempProject/nzca-wordpress/adverts-embedded-in-post-idea/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try replacing this:

    $output = "{$before}{$post->post_date}{$after}";

    with this:

    $output = "{$before}" . date('M j Y', strtotime($post->post_date)) . {$after}";
    Thread Starter Grant-Senior

    (@grant-senior)

    Hey thanks for putting your attention on this but that didn’t work, it generates the following to the page –
    date(‘M j Y’, strtotime(2014-02-01 01:24:27)).

    Sounds like you got some extra quotes in there somewhere. Please post the code you are using.

    Thread Starter Grant-Senior

    (@grant-senior)

    Here’s my function in functions.php with your line, mine commented out

    function mm_insert_date_func($atts) {
    
           global $post;
           extract(shortcode_atts(array('before' => '', 'after' => '' ), $atts));  
    
        //$output = "{$before}{$post->post_date}{$after}";
        $output = "{$before} . date('M j Y', strtotime($post->post_date)) . {$after}";    
    
           return $output;
        }
        add_shortcode('mm-insert-date', 'mm_insert_date_func');

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Sorry about that! Try this instead:

    $output = $before . date('M j Y', strtotime($post->post_date)) . $after;
    Thread Starter Grant-Senior

    (@grant-senior)

    That did the trick.
    Thank you for your help!

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with a similar question can see that there is a solution.

    Thread Starter Grant-Senior

    (@grant-senior)

    Done. Thanks again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How do I format my shortcode date?’ is closed to new replies.