在 WPBeginner,我们显示所有文章的最后修改日期而不是原始发布日期。我们认为从 wordpress 博客文章中删除日期是个坏主意。如果您使用上次修改日期,那么您可能希望在网站上显示最近更新的帖子列表。在本文中,我们将向您展示如何在 WordPress 中显示最近更新的帖子列表。
每次更新帖子时,WordPress 都会将该更新的日期和时间存储在帖子表中作为上次更新日期。我们将向您展示如何创建自定义 WordPress 查询来列出您最近更新的文章。
复制此代码并将其粘贴到特定于站点的插件或主题的functions.php文件中。
functionwpb_lastupdated_posts() { // Query Arguments$lastupdated_args= array('orderby'=> 'modified','ignore_sticky_posts'=> '1');//Loop to display 5 recently updated posts$lastupdated_loop= newWP_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 shortcodeadd_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
由
在 WordPress 中一键使用
就这样。现在,如果您想在主题的模板文件中显示最后更新的帖子,那么您可以像这样使用它:
<?php if(function_exists(wpb_lastupdated_posts)) : wpb_lastupdated_posts();endif;?>
由
在 WordPress 中一键使用
要显示 WordPress 帖子、页面和小部件中最新更新的帖子,您可以使用简码[lastupdated-posts]。
在 WordPress 中对文章进行排序有多种不同的方法。除了升序、降序和随机顺序之外,您还可以按过期日期显示帖子。通过本文,您现在可以按上次修改时间显示帖子。