[wp-hackers] Resetting after multiple loops
Nigel Dunn
nigel at redefine.co.uk
Sat Oct 25 15:54:55 GMT 2008
Viper007Bond wrote:
> My code makes no assumptions. You can use $foobar instead of $post if you so
> wish. I mean you're gonna end up doing setup_postdata($foobar); probably
> anyway.
Hmmm. Doesn't seem to work for me.
If I change wp_widget_recent_entries in wp-includes/widgets.php from:
$r = new WP_Query(...);
if ($r->have_posts()) :
...
while ($r->have_posts()) : $r->the_post();
...
endwhile;
...
wp_reset_query();
endif;
to be
$r = get_posts(...);
if ($r) :
foreach($r as $r_post) : setup_postdata($r_post);
...
endforeach
endif;
then the_title() & the_permalink() don't work as it repeats the values
for the post that you're viewing. It's the same result if I change
$r_post to $post. (And going back to my original question $id changes
its value after using the widget).
If I add in global $post; then the widget displays the correct results,
but the value of $post changes to the last post in the widget list.
Is there something I'm missing out - I can paste in the rest of the code
if necessary but it's the same between the two versions.
Cheers
Nigel
> On Sat, Oct 25, 2008 at 6:25 AM, Otto <otto at ottodestruct.com> wrote:
>
>> Even your example code makes the rather large assumption that you are
>> in the global variable context. Look at how you use $post. ;)
>>
>> My point is that no matter how you swing it, you tend to leave odd
>> side effects when messing with get_posts. Using a new WP_Posts doesn't
>> have those, generally speaking. Not unless you're using it wrong.
>>
>>
>> On Fri, Oct 24, 2008 at 9:31 PM, Viper007Bond <viper at viper007bond.com>
>> wrote:
>>> How so? It just returns an array that you can easily loop through. No
>> need
>>> to use have_posts(), the_post(), etc.
>>>
>>> On Fri, Oct 24, 2008 at 10:52 AM, Otto <otto at ottodestruct.com> wrote:
>>>
>>>> Creating a new WP_Query() seems like the better approach to me, as
>>>> that never touches the default query in any way.
>>>>
>>>> Whereas get_posts() can be confusing and difficult to use.
>>>>
>>>>
>>>>
>>>> On Fri, Oct 24, 2008 at 8:46 AM, Viper007Bond <viper at viper007bond.com>
>>>> wrote:
>>>>> Always use code like this if possible:
>>>>>
>>>>> $var_name_other_than_posts = get_posts( 'some=parameters' );
>>>>>
>>>>> foreach ( $var_name_other_than_posts as $post ) {
>>>>> // do stuff
>>>>> }
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>
>
>
--
Nigel Dunn
Redefine
0845 838 2720
http://www.redefine.co.uk
More information about the wp-hackers
mailing list