Skip to content

Instantly share code, notes, and snippets.

@generatepress
Last active January 23, 2019 07:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save generatepress/8bc94ae43f67c40a077aefc8ec970fd0 to your computer and use it in GitHub Desktop.
Save generatepress/8bc94ae43f67c40a077aefc8ec970fd0 to your computer and use it in GitHub Desktop.
Show date as: Published on {date} at {time} | Updated on {date} at {time}
add_filter( 'generate_post_date_output', 'tu_show_modified_date' );
function tu_show_modified_date() {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = 'Updated on: <time class="updated" datetime="%3$s" itemprop="dateModified">%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() )
);
// If our date is enabled, show it.
printf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
}
@brianleejackson
Copy link

brianleejackson commented Sep 7, 2017

Hey Tom! This is exactly what I need... however, I think you forgot about schema. Can you edit it and add the correct itemprop, etc... to it?

Thanks man.

@generatepress
Copy link
Author

Hey @brianleejackson - sorry I didn't see your comment!

I wonder if the above would work better? It should show the published date unless the post has been updated, at which point is shows the updated date.

Includes schema as well.

Let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment