最近在使用WordPress时用到wp_get_recent_posts函数,以前一直没用过,今天碰到一个问题,默认情况下,如果根据WordPress给的示例:
<h2>最新文章</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>最新发布文章</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参数就行。
温馨提示:本文最后更新于
2024-07-09 10:34:57
,某些文章具有时效性,若有错误或已失效,请 联系我们 反馈。👋 感谢您的观看!
© 版权声明
THE END