首页 > CMS教程 > WordPress    日期:2020-03-21 / 来自互联网 / 浏览

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参数就行。

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章