1

My code is based on the underscores reference theme. I have a posted_on function as follows:

function example_posted_on() {
        $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
            $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%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() )
);

    $posted_on = sprintf(
    ' %s',
    $time_string
);

echo '<span class="posted-on">' . $posted_on . '</span>';  //WPCS: XSS  OK.
}

The result is that example_posted_on() correctly displays only the published date. My questions are: Why does it get the modified date anyway? How is it possible to show the modified date in addition or instead the published date.

1 Answer 1

2

This function should be displaying both modified and published dates only if they differ.

    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }

Dates are compared using timestamps and if they don't match the string format is set to two <time> tags otherwise it's just published date.

6
  • I agree, but it doesn't. If I edit a post and hit the update button, the post just shows the initial date.
    – IXN
    Commented Apr 26, 2016 at 7:12
  • Is it an already published post or a draft?
    – Z. Zlatev
    Commented Apr 26, 2016 at 7:14
  • Already published.
    – IXN
    Commented Apr 26, 2016 at 7:15
  • 1
    @IXN it hidden by your css
    – Jevuska
    Commented Apr 26, 2016 at 9:33
  • @Jevuska That's probably it.
    – Z. Zlatev
    Commented Apr 26, 2016 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.