• Resolved judev

    (@judev)


    Hi there!

    Is there an easy way to get a shortcode for the published date of a blog post? on the free version of GP (I guess I should get the premium, I’m spending too much time looking for answers ().
    I want to display the published date and the updated date right after the header in a container.

    So far, what almost works :

    /* Display updated date */
    .posted-on .updated {
    display: inline-block;
    margin-right: 1em;
    }

    /*Add date prefixes */
    .posted-on .updated:before {
    content: ‘Updated: ‘;
    }
    .posted-on .entry-date:before {
    content: ‘Published: ‘;
    }

    which add the published and updated dates above the header (but i want to display it under).

    I was able to display the updated date :

    function post_modified_date() {
    return get_the_modified_date();
    }
    add_shortcode( ‘modified_date’, ‘post_modified_date’ );

    but nothing for the published date.
    I tried to use shortcodes as get_the_date, get_the_time, published_time but it doesn’t work. I also tried things like adding a get_the_date function to function.php but it’s not possible because this “function has been previously declared (in wp-includes/general-template.php:2527)”.

    I also tried different solutions I found on the forum without getting what I want.

    thanks a lot for your help!

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi there,

    I’m not sure why get_the_date() isn’t working for you.

    Can you try this?

    function shortcode_post_published_date(){
     echo '<time class="published-date">'.get_the_date('F d, Y').'</time>';
    }
    add_shortcode( 'post_published', 'shortcode_post_published_date' );
    Thread Starter judev

    (@judev)

    Hi!

    Thanks for your response!
    I added your function to functions.php and tried the shortcode [post_published] but it doesn’t work the way it should. When I try to update the post, it throws me an error “Updating failed. The response is not a valid JSON response.”
    I read about this error but didn’t investigate it too much. At this time, the only plugins i have are GenerateBlocks, Lightweight Social Icons, Image Optimisation by Optimole.
    Any ideas?
    Thanks!

    Thread Starter judev

    (@judev)

    I run a few tests and the error doesn’t seem to come from the plugins. I desactivated them all, clear the cache and I’m still facing the same error message.
    Although there is this error message, the page is actually updated.
    The shortcode [post_published] works but.. the published date is displayed at the beginning of the post.

    Strange. I don’t encounter the same issue when I tested.

    Can you try this?

    function shortcode_post_published_date(){
        ob_start();
        // Start your PHP below
      
        echo '<time class="published-date">'.get_the_date('F d, Y').'</time>';
      
        // End your PHP above
        return ob_get_clean();
     
    }
    add_shortcode( 'post_published', 'shortcode_post_published_date' );
    Thread Starter judev

    (@judev)

    Hi!
    This code gave me the same error.
    I finally found another code whick works :

    function ppd_shortcode(){
    return get_the_date();
    }
    add_shortcode(‘ppdate’, ‘ppd_shortcode’);

    I still don’t understand why the 2 others methods were throwing JSON error.

    Now… how do I remove everything in the top of the header? post date, updated date and author.

    Thanks!

    You can remove the entry meta items with this filter:

    add_filter( 'generate_header_entry_meta_items', function( $items ) {
        return array_diff( $items, array( 'author','date' ) );
    } );
    Thread Starter judev

    (@judev)

    Great, works very well!
    Thanks a lot ??

    No problem. ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display published & updated date in a container’ is closed to new replies.