0

I've taken a very minimalistic loop, and tried to modify it, so that I can use more then one loop on one page.

The minimalistic loop works fine and gets the content. But my modification to allow it to be re-used fails.

This is the working loop:

<?php

if (have_posts()) : 

?>

<?php while (have_posts()) : the_post(); ?>
    <?php the_content(); ?> 
<?php endwhile; ?>

All good so far. That actually works. But if I want to have more then one loop on a page, I'm forced to work with WP_Query.

So, I modify my loop thus:

<?php

$my_query = new WP_Query() ;

if ($my_query->have_posts()) : 

?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <?php $my_query->the_content(); ?> 
<?php endwhile; ?>

But now I get nothing back at all. I don't get any errors, and php after that loop continues to execute. But I'm not getting any content.

Where am I going wrong? How do I actually get the content?

1 Answer 1

1

You have to pass some arguments to new WP_Query or no database request will be sent. Your check …

if ($my_query->have_posts())

… will never evaluate to TRUE, because there are no data.

2
  • thanks. So now I'm stuck on which parameter to use. I just need the most minimal parameter set possible, in order to just get the actual content of the Page. I've been looking at the docs for WP_Query, but I can't figure this out. Any idea what my args should be?
    – bob
    Jan 18, 2013 at 3:33
  • @bob The content of the page is in $GLOBALS['post'].
    – fuxia
    Jan 18, 2013 at 5:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.