Hi
I want to add a shortcode to the views plugin to display the posts last modified date - can anyone advise me what the code should be?
Thanks
Dear rachelW,
To have more flexibility, you could do this with another shortcode (the following should be pasted in functions.php in current theme folder):
add_shortcode('wpv-post-modified', 'wpv_post_modified_shortcode');
function wpv_post_modified_shortcode($atts) {
if (empty($atts['format'])) {
$atts['format'] = get_option('date_format');
}
return get_the_modified_date($atts['format']);
}
And use it like this:
[wpv-post-modified format="d-m-Y"]
"format" argument will accept a formatting string following the php date formatting rules you can find here: hidden link, if omitted will take the format of your WordPress settings.
Please let me know if there is anything else that I can assist you with.
Thank you for that.
What I need to do, however, is return only the date of the most recently updated post within the types list. So I need to query the posts returning only the last modified post date, and then apply this function to that.
It is for a page that will display the 'last updated' date at the top of a list of posts that are in a custom order, so the last modified post will not necessarily be the first post returned by the query.
Can you advise me on how to do this?
Thanks
Dear rachelIW,
i modified a bit the previous function in order to display only the most recent modified date of a posts loop.
add_shortcode('wpv-post-modified', 'wpv_post_modified_shortcode');
function wpv_post_modified_shortcode($atts) {
static $tempDate = 0;
if (empty($atts['format'])) {
$atts['format'] = get_option('date_format');
}
if( $tempDate < get_the_modified_date('U', true) )
{
$tempDate = get_the_modified_date('U', true);
return date($atts['format'], $tempDate);
}
return '';
}
This will display only the date of the most recent modified post in a posts loop.
You can use it as the previous like so:
[wpv-post-modified format="d-m-Y h:i:s A (T)"]
Remember: in order for this t work as it is posts must be ordered by post_date descending order. Otherwise the function should be modified slightly.
Please let me know if there is anything else I can assist you with.
How would the code need to be modified for a loop of posts that are not ordered by post date?
I need the code to get the date only of the last modified post.... but these posts are looped in a custom order
I have now created a different function that returns the date I require... just in case anyone else is looking for something similar it is:
add_shortcode('custom-posttype-modified', 'custom_posttype_modified_shortcode');
function custom_posttype_modified_shortcode() {
query_posts(array('post_type'=>'custom-posttype','orderby' => 'modified', 'order' => 'DESC' , 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string = get_the_modified_date();
endwhile;
endif;
wp_reset_query();
return $return_string;
}
I know this is a really old post, but the example shortcode is still really helpful. I would just like to know how to display the words "Last modified" before the date and how to add a class.
I've been trying to find guidance in the codex but I'm struggling. Any guidance would e appreciated
add_shortcode('wpv-post-modified', 'wpv_post_modified_shortcode');
function wpv_post_modified_shortcode($atts) {
if (empty($atts['format'])) {
$atts['format'] = get_option('date_format');
}
return 'I WANT TO INSERT TEXT HERE' get_the_modified_date($atts['format']);}