Title: Help with category loop
Last modified: August 20, 2016

---

# Help with category loop

 *  Resolved [MrThunder](https://wordpress.org/support/users/mrthunder/)
 * (@mrthunder)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/)
 * Hi I am trying to get info from a category loop but I cannot get anything to 
   display.
 * Any advice would be appreciated, Thanks
 *     ```
       <?php 
   
                 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                 $my_query = new WP_Query(array('category__in' => array(4), 'posts_per_page' => 2,'paged'=>$paged)); ?>
                   <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) :the_post(); ?>
                           <?php $currentid = get_the_id(); ?>
   
                           <div class="project-box grid-box grid-block mod-box width100">
                                   <div class="project-img">
                                       <?php if (get_the_post_thumbnail($currentid) != "") { ?>
                                          <a href="<?php the_permalink(); ?>" ><?php echo get_the_post_thumbnail($currentid,array(300,600)); ?></a>
                                       <?php } else { ?>
                                           <a href="<?php the_permalink(); ?>" ><img src='<?php bloginfo('template_url') ?>/images/default.png' alt='Killen Civlil Engineering'/></a>
                                       <?php } ?>
                                   </div> 
   
                                   <div class="project-info">
                                       <h2><a href="<?php the_permalink(); ?>"><?php echo get_the_title($currentid); ?></a></h2>
                                       <p><?php echo get_the_excerpt(); ?></p>
                                        <a href="<?php the_permalink(); ?>" class="info">More  »</a>
                                   </div>
                                               </div> 
   
                       <?php endwhile; ?>
                     <div class="project-pagination">
   
                         <div class="pagination page-btn"><?php next_posts_link( '« Prev' ); ?></div>
                           <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »' ); ?></div>
   
                     </div>  
   
                   <?php
                   endif;
                   wp_reset_query();
       ?>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/#post-3505767)
 * you have missed to add `$my_query->` to `the_post();`
 * i.e. change the start of the loop to:
 *     ```
       <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * and replace
 *     ```
       wp_reset_query();
       ```
   
 * with
 *     ```
       wp_reset_postdata();
       ```
   
 *  Thread Starter [MrThunder](https://wordpress.org/support/users/mrthunder/)
 * (@mrthunder)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/#post-3505780)
 * Thank you very much, that worked great, except I now my pagination has disappeared.
 * Any suggestions?
 *     ```
       <?php 
   
                 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                 $my_query = new WP_Query(array('category__in' => array(4), 'posts_per_page' => 2,'paged'=>$paged)); ?>
                 <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
                           <?php $currentid = get_the_id(); ?>
   
                           <div class="project-box grid-box grid-block mod-box width100">
                                   <div class="project-img">
                                       <?php if (get_the_post_thumbnail($currentid) != "") { ?>
                                          <a href="<?php the_permalink(); ?>" ><?php echo get_the_post_thumbnail($currentid,array(300,600)); ?></a>
                                       <?php } else { ?>
                                           <a href="<?php the_permalink(); ?>" ><img src='<?php bloginfo('template_url') ?>/images/default.png' alt='Killen Civlil Engineering'/></a>
                                       <?php } ?>
                                   </div> 
   
                                   <div class="project-info">
                                       <h2><a href="<?php the_permalink(); ?>"><?php echo get_the_title($currentid); ?></a></h2>
                                       <p><?php echo get_the_excerpt(); ?></p>
                                        <a href="<?php the_permalink(); ?>" class="info">More  »</a>
                                   </div>
                             </div> 
   
                       <?php endwhile; ?>
                     <div class="project-pagination">
   
                         <div class="pagination page-btn"><?php next_posts_link( '« Prev' ); ?></div>
                           <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »' ); ?></div>
   
                     </div>  
   
                   <?php
                   endif;
                   wp_reset_postdata();
       ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/#post-3505812)
 * pagination:
 *     ```
       <div class="pagination page-btn"><?php next_posts_link( '« Prev', $my_query->max_num_pages  ); ?></div>
                           <div class="pagination page-btn-1"><?php previous_posts_link( 'Next »', $my_query->max_num_pages ); ?></div>
       ```
   
 * [http://wordpress.stackexchange.com/questions/20424/wp-query-and-next-posts-link](http://wordpress.stackexchange.com/questions/20424/wp-query-and-next-posts-link)
 *  Thread Starter [MrThunder](https://wordpress.org/support/users/mrthunder/)
 * (@mrthunder)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/#post-3505855)
 * That worked great thanks!! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Help with category loop’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 4 replies
 * 2 participants
 * Last reply from: [MrThunder](https://wordpress.org/support/users/mrthunder/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/help-with-category-loop/#post-3505855)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
