Tony
Tony

Reputation: 27

Display last date a post was updated on WordPress

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:

Screenshot

Upvotes: 2

Views: 1872

Answers (5)

Abe
Abe

Reputation: 431

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)

<?php 
        $x = get_the_time('U');             
        $m = get_the_modified_time('U');    
        if ($m != $x) { 
            $t = get_the_modified_time('F d, Y');
            echo "<p class=\"lastupdated\">Updated on $t </p>"; 
        } 
    ?>

For example, suppose that your post template is called content-post.php. Look for a part that looks like the below:
Snippet (2)

            // Post date
            if ( in_array( 'post-date', $post_meta_top ) ) : ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>
            <?php endif;

Insert snippet (2) immediately before the closing tag of snippet (1) as follows:

        <?php

        // Post date
        if ( in_array( 'post-date', $post_meta_top ) ) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>

        <?php 
            $x = get_the_time('U');             
        $m = get_the_modified_time('U');    
        if ($m != $x) { 
        $t = get_the_modified_time('F d, Y');
        echo "<p class=\"lastupdated\">Updated on $t </p>"; 
            } 
            ?>


        <?php endif;

Now, you will get the original post date and the updated post date. Special thanks go to @Sebastian Paaske Tørholm for his comment.

Upvotes: 0

Zohair Baloch
Zohair Baloch

Reputation: 21

You can also display the last modified time of a post through this code by placing it anywhere in your loop.

Posted on <?php the_time('F jS, Y') ?>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if($u_modified_time != $u_time) {
    echo "and last modified on ";
    the_modified_time('F jS, Y');
    echo ". ";
} ?>

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

     <?php 
            $x = get_the_time('U');             
            $m = get_the_modified_time('U');    
            if ($m != $x) { 
                echo "<p class=\"lastupdated\">Updated on ".the_modified_time('F d, Y')."</p>"; 
            } 
        ?>

Upvotes: 0

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:

$t = get_the_modified_time('F d, Y');

Upvotes: 4

Jakub Hampl
Jakub Hampl

Reputation: 40563

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.

Upvotes: 0

Related Questions