[wp-hackers] Resetting after multiple loops
nigel at redefine.co.uk
Fri Oct 24 08:40:52 GMT 2008
I've been writing a number of widgets based on the style of the recent
posts widget that comes with WP - e.g. listing the most recent posts by
an author, for a category, etc. But I've come unstuck with side-effects
on other globals even though I'm using what seems to be the suggested
pattern of using 'new WP_Query(...)' followed by wp_reset_query().
I think the problem is caused by the_post(), but I can't see anything
documented about resetting globals after you've used that.
If you try the following on a fresh install of WP:
* create a new post
* add the recent posts widget to the sidebar
* change sidebar.php in the default theme to display the value of $id
before & after the sidebar contents
* change single.php in the default theme to use the sidebar
When I view the new post on the website I have different values for $id.
If I comment out the loop that's used in the widget then they're the
same. If I stick something like 'if (have_posts()) the_post()' at the
end of the sidebar then I also get the same values.
So the sort of problem I've been having is that if I have another widget
that's supposed to display the details of the post author then it could
change if I place that before or after the recent posts widget.
Is this a bug or am I missing out on something else? (And should I be
posting this elsewhere?)
0845 838 2720
More information about the wp-hackers