Show ‘Last updated date’ below H1
-
Hello,
Is there a way to show the last updated date before the featured photo? (Right underneath the published date) example: https://passportandplates.com/destination-guides/best-hotels-cairo-where-to-stay-cairo-egypt/Also, if the publish date and modified date are the same, is there a way to hide the modified date?
Thank you!
The page I need help with: [log in to see the link]
-
Hi
You need to edit your theme files. Add this snippet to the end of your child theme’s function.php file:
function flatsome_posted_on() { $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>'; $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); $type = 'Posted on '; if ( function_exists( 'get_the_last_modified_info' ) ) { if ( get_the_time( 'U' ) < get_the_modified_time( 'U' ) ) { $time_string = get_the_last_modified_info(); $type = 'Updated on '; } } $posted_on = $type . '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'; $byline = sprintf( esc_html_x( 'by %s', 'post author', 'flatsome' ), '<span class="meta-author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>' ); echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; }This snippet will print Published date by default. If you modify any post, then it will print Modified Date. Don’t forget to configure the plugin settings.
Thanks!
Hello,
Thanks for sending and your quick reply!
When I try to paste the code, it says syntax error, unexpected ‘=’
It appears to be referring to this line : $time_string = sprintf( $time_string,
Is there something I can do to fix this?
Hi @sallywp
Try to use this:
function flatsome_posted_on() { $time_string = '<time class="entry-date published" datetime="' . esc_attr( get_the_date( 'c' ) ) . '">' . esc_html( get_the_date() ) . '</time>'; $type = 'Posted on '; if ( function_exists( 'get_the_last_modified_info' ) ) { if ( get_the_time( 'U' ) < get_the_modified_time( 'U' ) ) { $time_string = get_the_last_modified_info(); $type = 'Updated on '; } } $posted_on = $type . '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'; $byline = sprintf( esc_html_x( 'by %s', 'post author', 'flatsome' ), '<span class="meta-author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>' ); echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; }Thanks!
Thank you!
I was able to update the code.
However, it’s now showing just the ‘updated on’ date. Is it possible to show both the ‘published on’ and the ‘updated on’ date?
So it would be
H1 Title
published on [date]
last updated on [date] (only if the date is different)then the featured image, etc.
Thanks very much much for your help!
Replace this line
$time_string = get_the_last_modified_info();With this
$time_string .= ' | ' . get_the_last_modified_info();Thanks!
Hi @sallywp
Try to use this and ignore my previous code:
function flatsome_posted_on() { $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>'; $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); $posted_on = 'Posted on ' . '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'; if ( function_exists( 'get_the_last_modified_info' ) ) { if ( get_the_time( 'U' ) < get_the_modified_time( 'U' ) ) { // 86400 seconds i.e. 24 hours, set it according to your need $posted_on = $posted_on . ' | Updated on <a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . get_the_last_modified_info() . '</a>'; } } $byline = sprintf( esc_html_x( 'by %s', 'post author', 'flatsome' ), '<span class="meta-author vcard"><a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>' ); echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; }Thanks!
That worked PERFECTLY. Thank you so much!
I’m marking this as resolved. Again, thanks for your help!
The topic ‘Show ‘Last updated date’ below H1’ is closed to new replies.