0

i have a wordpress query:

$myPosts = new WP_Query();
$myPosts->query($queryArray);

How do i display these according to the loop template. I'm already inside a loop so this isn't working:

print get_template_part('loop');

1 Answer 1

2

Looping within a Loop is possible, and while you can technically still use your template loop.php, it's a bit of a pain because you have to set up the query differently. You're best of just leaving it in the file, or creating a function and passing the query to that function.

Any way, back to the question. First of all you need to make a copy of the current query so that you can go back to it after the second query -

$query_safe = $wp_query;

Now you need to setup your new loop and output what ever you want within it -

if($myPosts->have_posts()) : while($myPosts->have_posts()) : $myPosts->the_post();

        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>

    endwhile;
endif;

Finally, when you are done, restore your original query so that WP can continue with that loop.

$wp_query = $query_safe;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.