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参数就行。
相关阅读:
本文《wp_get_recent_posts函数排除显示》由网友投稿或:「admin」整理自网络。
转载请声明来自:云猴子 - https://www.yunhouzi.com/463.html
1,本站所有资源均来源于用户上传或整理与网络,如有侵权请【内容投诉】删除,我们将及时处理!
2,本站资源仅供大家学习和交流,请不要用于商业用途,下载后请于24小时后删除!
3,如果你也有好的资源,可以投稿到本站,有金币奖励和额外的收入!
4,从您进入本站开始,已表示您已同意接受本站【版权声明】中的一切条款!
6,申明:本站资源出售只是赞助,仅用于本站服务器和日常运营所需!不提供任何技术支持。
7,如压缩包提示有密码,www.yunhouzi.com
云猴子 » wp_get_recent_posts函数排除显示