🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

WordPress Call to Display Recently Modified/Updated Articles

Yesterday, we sharedDisplaying the Last Modified Time of Articles on WordPress Article PagesSo, how can people know which articles have been modified or updated? Of course, by displaying them on the front end. Therefore, this article teaches you how to call and display a list of the most recently updated articles on WordPress.

Code Addition Method

Copy the code below and add it to your current theme's functions file, or you can use a plugin method:Safe Method to Add Code to the functions.php File: Code Snippets
//显示最后更新的文章https://blog.naibabiji.com/skill/wordpress-xian-shi-zui-hou-geng-xin-shi-jian.html
function wpb_lastupdated_posts() { 
 
// 查询参数
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);
 
//显示最后更新的5篇文章
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<div class="widget-lastupdated">';
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
$string .= '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile; 
$string .= '</ul>';
$string .= '</div>';
return $string;
wp_reset_postdata(); 
} 
 
//添加一个短代码
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
The class 'widget-lastupdated' in the code above is added for you to customize styles. If not needed, simply delete that part of the code.
//不需要定制css样式就删除这两行
$string .= '<div class="widget-lastupdated">';

$string .= '</div>';
After adding the code, here is how to call it:

Front-end Calling Method

To call it within a template, use the following code:
<?php 
if (function_exists(wpb_lastupdated_posts)) : 
wpb_lastupdated_posts();
endif;
?>
Calling in Widgets or ArticlesUse the shortcode method:
[lastupdated-posts】
Replace the 】 at the end of the shortcode above with ] when using it.

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: