wp_reset_query函数的作用

在wordpress中很多时候都会用到query_posts()函数来设置loop循环的查询条件,而wp_reset_query()可以看作是和query_posts()同时出现的配对函数。也就是说开头出现query_posts(),末尾必然会有wp_reset_query()。

wp_reset_query()函数的作用就是重置一个query,如果不使用,那么其它的查询调用可能会出错。

实例:

<?php
query_posts( 'posts_per_page=5' );
if ( have_posts() ) :
	while ( have_posts() ) : the_post();
		?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
	endwhile;
endif;
wp_reset_query();
?>

该代码的作用是显示最新的5篇文章那个,并在末尾用wp_reset_query重置query。