Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

insert second loop then resume previous loop

Hey all,

A PHP question for you. i am working with a wordpress theme, and I have added the plugin " PHP code for posts. I have added a shortcode snippet in here that calls a loop and then ends with "endwhile; endif;" problem is, that my sidebar is now how showing properly and I am assuming it is because I stopped the previous loop. Is there a way to resume the previous loop after I have open and closed a secondary loop?

Thanks! much appreciated.

5 Answers

The sidebar should not be part of a loop, I'm confused how your code it working. The way you describe it, the sidebar contains and entirely separate loop. You have to reset the default query if you're doing a non-default loop but that shouldn't affect something like your sidebar showing up.

My guess is you have syntax problems elsewhere.

That makes sense. However, in the theme I am working with, I specific a custom sidebar in the theme page itself. So I guess the reference comes from the page loop in this case. There is a conflict somewhere, I assumed it had to do with the loop stopping and starting.

I still don't think the sidebar should be part of a loop or else it would show up X number of times depending on how many items the query was returning. I'd have to see the theme code to really tell you how it's working exactly.

You should review the WP Query docs as it gives a good overview of how queries work in WP. If you're in a secondary query and you're using the_post() function, you do have to reset the query in order to use the_post() elsewhere on the page, but I still am not sure that's what's causing your issue.

yahoo! that worked! can't believe I missed that. wp_reset_postdata(); was all it needed.

Thanks Paul!

Great, just remember the_post() is only in reference to the active query object. If you use a secondary loop, you change that active query object and will have to reset.

Just to be safe I always reset every WP_Query anyway.