开发者

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 tim开发者_C百科e 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:

Display last date a post was updated on WordPress


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');


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.


     <?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>"; 
            } 
        ?>


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 ". ";
} ?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜