wp_reset_query()恢复$post全局变量,销毁之前的查询

目录

描述

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.

Calling wp_reset_query is not necessary after using WP_Query or get_posts as these don't modify the main query object. Instead use wp_reset_postdata

wp_reset_query() 描述

用法

<?php wp_reset_query(); ?>

wp_reset_query() 用法

参数

This function does not accept any parameters.

wp_reset_query() 参数

返回值

This function does not return any values.

wp_reset_query() 返回值

示例

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.

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.

wp_reset_query() 示例

历史

  • 添加于 版本: 2.3.0

wp_reset_query() 历史

源文件

wp_reset_query() 函数的代码位于 wp-includes/query.php.

wp_reset_query() 源文件

相关