WordPress: Display a List of Last Updated Posts

If you are using last modified date, then you might want to show a list of your recently updated posts on your site. Each time you update a post, WordPress stores the date and time of that update in the posts table as last updated date. We will show you how to create a custom WordPress query to list your most recently updated articles.

Copy code below to your [highlight color=”orange”]functions.php[/highlight]

function wpb_lastupdated_posts() { 

// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'

//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
echo '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
echo '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
echo '</ul>';

//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');

That’s all. Now if you want to display last updated posts in your theme’s template files, then you can use it like this:

if (function_exists(wpb_lastupdated_posts)) : 

And if you need to change in the posts or pages then you need to open these files: [highlight color=”orange”]index.php[/highlight] or [highlight color=”orange”]home.php[/highlight], [highlight color=”orange”]single.php[/highlight] and [highlight color=”orange”]page.php[/highlight]. But sometimes theme developer change this file with template, like [highlight color=”orange”]loop.php[/highlight] inside include folder.

Then you will need to locate the following code:

<?php the_modified_time('F jS, Y');?>

Replace it with:

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

Or maybe you want to not change the original post date instead add last updated info below post date, you could try WordPress plugin Last Updated Shortcode.


source: wpbeginner

Artikel terkait:   WordPress: Memperbaiki Post Editor (TinyMCE)

Isi Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

× five = twenty

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top