Yesterday, we shared
Displaying 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.
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.