在 WordPress 裏有一個小工具,顯示最新文章,不過這個是按最後發佈的日期顯示的。另外在 WordPress 循環中,也是按最後發佈的日期顯示的。但是對於有限項目中需要按最新更新的方式顯示文章。對此,是必要的。能夠有利於讓用户更好的獲取網站的第一手資訊。所以按最新更新文章顯示是非常好的。下面提供一個函數:
把下面的代碼放在 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;
echo '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
echo '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile;
echo '</ul>';
wp_reset_postdata();
}
//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
現在就可以在 WordPress 主題模版中使用下面的方式顯示最新更新的文章了:
<?php if (function_exists(wpb_lastupdated_posts)) : wpb_lastupdated_posts(); endif; ?>
如果不想修改 WordPress 主題模版文件,也可以直接在文章、頁面、小工具裏直接添加簡碼:
[lastupdated-posts]