I’m trying to figure out how to display the date that a post was last updated outside of the loop. I’m able to display the published date using get_the_time()
but there doesn’t seem to be a “loopless” function to get the date of the last update.
Does anyone know hot to do this?
It is unclear if you are looking for the last updated post or for the last updated date for some particular post. The answer by @PatJ assumes the former. To do the latter:
Or…
Of course you need to change the post ID to match the post you are looking for.
According to the Codex page for
get_the_time()
, it needs to be used in The Loop. The difference betweenthe_time()
andget_the_time()
is that the formerecho()
es the date, and the latter returns it.There are a couple functions that do what I think you’re looking for — get the last updated date and time for a post:
get_the_modified_time()
andget_the_modified_date()
. It looks like they too need to be used in The Loop.Here’s one way to get the updated date of the most recent post in your site:
If you’re sure you need to be outside of any Loops, you can always use
$wpdb
:A late addition, but the following snippet can be modified to use most functions outside of the loop:
Another late addition, which may be helpful to know if anyone is looking. You can use these two functions to get the post date and modified date outside the loop.
and
You can read more about these two functions in the codex.
http://codex.wordpress.org/Template_Tags/get_post_time
http://codex.wordpress.org/Function_Reference/get_post_modified_time
It may not be clear from previous answers, but get_post_time() and get_post_modified_time() can be given a post object or post ID. So, to get the published and modified dates by post ID outside the loop:
js.
What about the
get_lastpostmodified( $timezone )
function?