wp_get_recent_posts函数排除显示

来自:互联网
时间: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参数就行。

返回顶部
顶部