Home Experience & Tips Sharing Standard Post

Standard Post

WordPress Call to Display Recently Modified/Updated Articles

Yesterday, I shared a method to display the last modified time on WordPress article pages. So how do people know that these updated articles have been modified? Of course, by displaying them on the front end. Therefore, this article teaches you how to call and display a list of recently updated articles on WordPress. Code addition method: Copy the following code and add it to your current theme's...

Updated on November 1, 2019 About 3 minutes reading
WordPress调用显示最后修改更新的文章

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.

Rate this article post
Previous How to Display the Last Updated Time for Posts in WordPress Continue reading content around the same timeline. Next How to Reduce the Impact on SEO When Changing a Website's Theme Template View the next related tutorial or experience.

AI Website Building Assistant

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