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.

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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