wp_get_recent_posts函数最近才开始用,以前一直没用过,今天碰到一个问题,默认情况下,如果根据官方给的示例:
<h2>Recent Posts</h2> <ul> <?php $recent_posts = wp_get_recent_posts(); foreach( $recent_posts as $recent ){ echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> '; } wp_reset_query(); ?> </ul>
直接这么用的话,默认会将计划任务文章显示出来,而如果使用WP_Query或query_posts方法都不会出现这种错误。这样显示是有问题的,生产环境必须只能显示已经发布的文章,你必须要在参数中指定状态:
<h2>Recent Posts</h2> <ul> <?php $args = array( 'numberposts' => '5','post_status' => 'publish', ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> '; } wp_reset_query(); ?> </ul>
指定post_status参数就行。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)