Function Reference/wp reset query

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.

Usage

%%%<?php wp_reset_query(); ?>%%%

Parameters

This function does not accept any parameters.

Return Values

This function does not return any values.

Examples

The following example shows how to use wp_reset_query() after a custom loop. Note that the loop in the example is probably being used in addition to the main loop.

<?php

$args = array ( 'post_parent' => 5 );
$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ):
    while ( $custom_query->have_posts() ) :
        $custom_query->the_post();

        // Do stuff with the post content.
        the_title();
        the_permalink(); // Etc.

    endwhile;
else:
    // Insert any content or load a template for no posts found.
endif;

wp_reset_query();

?>

query_posts() will change your main query and is not recommended. Only use if absolutely necessary (see query_posts: Caveats). Creating a new instance of WP_Query or get_posts() is preferred for secondary loops. If you would like to modify the main query, use the pre_get_posts action. Be sure to put your pre_get_posts filtering in your functions.php file.

<?php
query_posts( 'post_parent=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();
?>

Change Log

Source File

wp_reset_query() is located in onlyincludecodewp-includes/query.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

get_posts, query_posts, rewind_posts, wp_reset_query

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude