Archived topic
Showing last updated and posted date + date format
15 replies · Started by Antoinette on December 8, 2020
Hello again :-)
Two questions, both relating to dates on posts.
1. I want to include the last updated time stamp in the post header as well as the published on date stamp in the header. Do I need to add a filter to generate the last updated time stamp?
2. For reasons known only to itself (possibly AMP related...), despite changing the settings in wordpress itself, the weekdays / months are not being displayed as full names. Any ideas where I might have added something I should not? Or is there a setting in cpanel that might have gone rogue?
For reference, here's a link to a post on the site I'm working on.
Hi,
1. I want to include the last updated time stamp in the post header as well as the published on date stamp in the header. Do I need to add a filter to generate the last updated time stamp?
Yes you'll need a filter for that.
Here's an example snippet:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
if ( get_the_date() !== get_the_modified_date() ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time> | <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html__( 'Published on:', 'generatepress' ).' '.esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html__( 'Last Updated on:', 'generatepress' ).' '.esc_html( get_the_modified_date() )
);
return sprintf ( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
This snippet displays the modified date only if it exists(if the post is modified). Else, it'll just display the post date.
2. For reasons known only to itself (possibly AMP related…), despite changing the settings in wordpress itself, the weekdays / months are not being displayed as full names. Any ideas where I might have added something I should not? Or is there a setting in cpanel that might have gone rogue?
From the code provided above, you can play around with the get_the_date( 'c' ) and get_the_modified_date( 'c' ) line.
https://developer.wordpress.org/reference/functions/get_the_date/
The 'c' within the function is the date format. Change it to your preference.
Here's are the date formats you can use:
https://www.php.net/manual/en/function.date.php
Thank you! Since I'm using the header template element, is there a {{post_date_modified}} output or similar I can insert in there? Or must it all go on as a hook via Elements?
Oh right if that's the case, you can use this documentation as reference for styling:
https://docs.generatepress.com/article/page-hero-examples/
As for the time, you'll have to make a shortcode that displays it.
Here's an example:
https://docs.generatepress.com/article/show-the-updated-post-date/#show-the-%E2%80%9Cupdated%E2%80%9D-post-date-in-header-element
And since the shortcode example provided uses get_the_modified_date( ) too, you can adjust the shortcode to display the preferred format.
You can do this for the post date as well.
Thanks! I'll try that - only possible challenge - AMP doesn't like shortcodes and the site is running on AMP standard mode. Insert that grimmace emoji here.
I'll test though and let you know.
No problem. Let us know how it goes. :)
Seems AMP is a bit more tolerant of shortcodes these days.
The date format issue was related to the site language as an FYI. It was set to my localised English version and would not display the date format correctly, despite using the right PHP format. Switched to UK English and updated translations and it works as expected now.
Thanks for your help as always.
Good find. Thank you for sharing this finding with us.
Thanks for your help as always.
No problem. Glad to be of any help. :)
Hopefully that info about the date format helps someone else, saw on some of the WP forums that the issue is still present in a few translations.
Sidenote, but related: For generating shortcodes to use as hooks (so for post terms, dates, etc) is there a list somewhere for easy reference?
Sidenote, but related: For generating shortcodes to use as hooks (so for post terms, dates, etc) is there a list somewhere for easy reference?
The best reference for things like this is the official WordPress Codex.
For creating shortcodes.
https://developer.wordpress.org/reference/functions/add_shortcode/
For executing shortcodes using PHP snippets.
https://developer.wordpress.org/reference/functions/do_shortcode/
But in GP Premium's case, you should be able to use Hooks Element to run your shortcode on a specified hook.
Perfect! Thank you.
Perfect! Thank you.
No problem.
To add: This is pretty useful too as its functions are used in WordPress.
PHP manual
https://www.php.net/manual/en/index.php
I love and hate this theme at the same time for 'making' me learn so much php and CSS.
I was only just beginning to learn some Javascript (because it's the easiest language obviously....) and yet here we are, learning how to code if false parameters and whatnot.
:-)
I love and hate this theme at the same time for ‘making’ me learn so much php and CSS.
I was only just beginning to learn some Javascript (because it’s the easiest language obviously….) and yet here we are, learning how to code if false parameters and whatnot.
The payoff for learning PHP is really high as it deals directly with how the markup is being displayed. It is basically what makes the site while CSS and JS are the one that makes it pretty. :)
Glad you're learning a lot. It's always a nice feeling to learn something. :D
That's true. I'm a bit stuck now though.
I'd like to generate a shortcode for tags and categories to use at the bottom of posts. I know they display by default, but I want to use the block element to combine the author box, tags and categories as well as the share this social buttons all in one block.
Please can you help me with the snippet to generate both tags and catagories as an output? Clickable, but no prefix like "tags" or whatever. Hope that makes sense.