将以下代码加入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 < 7 ) : $lastupdated_loop->the_post();
echo '<li> <a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a><br> ( 更新时间:'. get_the_modified_date('Y.m.d G:i') .') </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]


