Very short trick: WordPress, reset a query

I often happen to customize the home page ( index.php ), or other sections such as the Sidebar, a Web site made ​​with WordPress. Many of the customizations affect loop with specific parameters, for example in order to view the last items of a specific category or tag. I carry out this operation, almost always using the query_posts() . o is_page() . When you run a query_posts() are also affected some global variables, and often annoying when you have behavior - immediately after - using features such as is_category() or is_page() . . Since the release of WordPress 2.3.0 is available a convenient function that has exactly the task of the state resetting altered by query_posts() : wp_reset_query() .
You just call the loop at the end of our own, so as to leave no trace in the system and thus do not affect other functions that refer to global variables used by query_posts() :

1
2
3
4
/ / A hypothetical example in index.php
/ / End of page, after performing one or more
/ / Query_posts () personally
; get_sidebar ( ) ; get_footer ( ) ; ?> <? Php wp_reset_query (); get_sidebar (); get_footer ();?>

A comment: "Very short trick: WordPress, reset a query"

  1. January 5, 2010 Francis:

    I was really very auito your post. :-) thanks

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL