如何在WordPress中显示最近更新的帖子列表

At WPBeginner, we show last modified date instead of original published date for all of our articles. We think it is bad idea to remove dates from your WordPress blog posts. If you are using last modified date, then you might want to show a list of your recently updated posts on your site. In this article, we will show you how to display a list of last updated posts in WordPress.

在WPBeginner,我们显示所有文章的最后修改日期,而不是原始发布日期。 我们认为从WordPress博客帖子中删除日期是个坏主意。 如果您使用的是上次修改日期,则可能要显示站点上最近更新的帖子的列表。 在本文中,我们将向您展示如何显示WordPress中最近更新的帖子列表。

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.

每次更新帖子时,WordPress都会将该更新的日期和时间存储在帖子表中,作为上次更新日期。 我们将向您展示如何创建自定义WordPress查询以列出您最近更新的文章。

Copy and paste this code in a site-specific plugin or your theme’s functions.php file.

将此代码复制并粘贴到特定站点的插件或主题的functions.php文件中。



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;
$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>';
return $string;
wp_reset_postdata(); 
} 

//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:

就这样。 现在,如果要在主题的模板文件中显示最近更新的帖子,则可以这样使用它:


<?php 
if (function_exists(wpb_lastupdated_posts)) : 
wpb_lastupdated_posts();
endif;
?>

To display last updated posts in WordPress posts, pages, and widgets, then you can use the shortcode [lastupdated-posts].

要在WordPress帖子,页面和小部件中显示最近更新的帖子,则可以使用简码[lastupdated-posts]

There are many different ways to sort your articles in WordPress. Aside from the ascending, descending, and random order, you can also display posts by expiration date. With this article, you can now show posts by the last modified time.

有许多不同的方法可以在WordPress中对文章进行排序。 除了升序,降序和随机顺序外,您还可以按到期日期显示帖子 。 通过本文,您现在可以按上次修改时间显示帖子。

How would you use this on your site? Are you displaying original published date or last modified date? Let us know by leaving a comment below.

您将如何在网站上使用它? 您显示的是原始发布日期还是上次修改日期? 让我们在下面留下评论。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-last-updated-posts-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值