WordPress 显示刚刚更新的文章 | 祭夜博客
  • 欢迎光临,这个博客颜色有点多

WordPress 显示刚刚更新的文章

Wordpress msojocs 7年前 (2017-02-18) 3895次浏览 已收录 1个评论 扫描二维码

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 < 7 ) : $lastupdated_loop->the_post();
echo '<li>&nbsp;&nbsp;&nbsp;<a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a><br>&nbsp;&nbsp;&nbsp; ( 更新时间:'. 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]


祭夜の咖啡馆 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress 显示刚刚更新的文章
喜欢 (6)
[1690127128@qq.com]
分享 (0)

您必须 登录 才能发表评论!

(1)个小伙伴在吐槽
  1. 博主的文笔很不错,仰慕已久,特来拜会。若能有幸能得到博主的回访,必将欣喜若狂
    馨香羽灵2017-02-24 12:41 Windows 7 | Chrome 31.0.1650.63