I’m using some PHP to display the last time a blog post was updated on WordPress using the get_the_time
and get_the_modified_time
functions. However, I can’t get the last modified time to display inline in the paragraph.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = the_modified_time('F d, Y');
echo "<p class="lastupdated">Updated on $t </p>";
}
?>
Here’s a screenshot of the result:
the_modified_time
prints the last modification time, so it prints it before you print your<p>
.Instead you’ll want to use
get_the_modified_time
for setting$t
, like so:It’s probably because these functions don’t return the result but
echo
it directly. Thus you must call them at the spot you need them to be.You can also display the last modified time of a post through this code by placing it anywhere in your loop.
This is the complete code based on the above comments. Now it works and you just need to add this to your post template:
Snippet (1)
For example, suppose that your post template is called
content-post.php
. Look for a part that looks like the below:Snippet (2)
Insert snippet (2) immediately before the closing tag of snippet (1) as follows:
Now, you will get the original post date and the updated post date. Special thanks go to @Sebastian Paaske Tørholm for his comment.