0

I have 3 divs that WP_Query with 3 different categories and end it with the php endwhile,but it only shows the same most recent post query three times over. Do I need to hard reset the query loop somehow? I'm looking to see where to put wp_reset_postdata...

<div>
    <?php $test = new WP_Query('category=[errands]&showposts=1');
    while ($test->have_posts()) : $test->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <img src="<?php bloginfo('template_directory'); ?>/images/errands.jpg" />

        <?php the_content(__('Read more'));?>
        <?php endwhile;?>
        <?php wp_reset_postdata(); ?>

</div>

 <div>
    <?php $test2 = new WP_Query('category=[shopping]&showposts=1');
    while ($test2->have_posts()) : $test2->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <img src="<?php bloginfo('template_directory'); ?>/images/shopping.jpg" />

        <?php the_content(__('Read more'));?>
        <?php endwhile;?>
        <?php wp_reset_postdata(); ?>

</div>

 <div>
    <?php $test3 = new WP_Query('category=[calendar_maintenance]&showposts=1');
    while ($test3->have_posts()) : $test3->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <img src="<?php bloginfo('template_directory'); ?>/images/calendar.jpg" />

        <?php the_content(__('Read more'));?>
        <?php endwhile;?>
        <?php wp_reset_postdata(); ?>

</div>
  • 1
    Did you try wp_reset_postdata() (see Codex here) ? ps: posts_per_page is the new showposts. – birgire Apr 2 '14 at 18:12
  • No I was using the wp_reset_query. I will try <?php wp_reset_postdata(); ?> now. And see if it works. – JBrady_IzenSoftware Apr 2 '14 at 18:16
  • The categories are the tax slugs so i will look into query_var true do wp_reset_postdata only once – JBrady_IzenSoftware Apr 2 '14 at 18:37
  • @G.M. when I use 'cat=1' instead of category=[errands] the post doesn't even show. Is there something wrong with my category – JBrady_IzenSoftware Apr 2 '14 at 18:54
  • I'm lost: you are using categories or custom taxonomies? They are not same thing. – gmazzap Apr 2 '14 at 19:06
0

If you want your posts based on different categories you should use category_name or cat as category id. For example;

<?php $test = new WP_Query('cat=1&showposts=1'); // where 1 is the id of your cat ?> 

Or

 <?php $test = new WP_Query('category_name=errands&showposts=1'); ?>

Also, you need to call wp_reset_postdata(); after each WP_Query. I hope this helps

More on how to use categories in WP_Query

http://codex.wordpress.org/Class_Reference/WP_Query

  • Undeletung since per discussion custom taxonomies are not relevant. – Rarst Apr 2 '14 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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