Title: Nested loop: foreach category &gt; foreach custom post in that category
Last modified: August 22, 2016

---

# Nested loop: foreach category > foreach custom post in that category

 *  [abtx](https://wordpress.org/support/users/abtx/)
 * (@abtx)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/foreach-slider/)
 * Hi,
    I’m gonna cut to the chase. The pseudocode looks like this: > for each existing
   category > wrap in a slider markup > for each custom post type with that category
   > write in slide markup
 * Effectively, this should get me as many sliders as categories, with as many slides
   as custom post types in that category. The code so far outputs all custom category
   posts in one slider. How can I get a slider for each category?
 *     ```
       <?php $categories=get_categories(); foreach($categories as $category) { ?>
   
       <?php $catname=g et_category( 'Category1'); echo '<p>' . $catname . '</p>'?>
   
       <!-- et_pb_row > container -->
       <!-- <a class="arrow-left" href="#">left</a>
       							<a class="arrow-right" href="#">right</a> -->
   
       <!-- swiper for each category -->
       <div class="swiper-container">
           <div class="swiper-wrapper">
   
               <!-- query that category -->
               <?php query_posts( 'post_type=human_section&category=' ); ?>
   
               <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
               <div class="entry-content swiper-slide">
                   <?php the_content(); ?>
   
               </div>
               <!-- .entry-content -->
   
               <?php endwhile; endif; ?>
               <!-- <?php wp_reset_query(); ?> -->
               <?php rewind_posts(); ?>
   
           </div>
           <!-- .swiper-wrapper -->
       </div>
       <!-- .swiper-container -->
       <div class="pagination"></div>
   
       <? } // foreach($categories ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [abtx](https://wordpress.org/support/users/abtx/)
 * (@abtx)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/foreach-slider/#post-5255078)
 * I take this pseudocode should look something like this in wp:
    get all categories
   with custom post type of x for each category query all posts
 * I’m not sure if this is the way to go in WordPress, I take that I have to use
   foreach to create a slider for each category, and then only I can query all posts
   in that category. I have problem determining how to use the array in foreach(
   $categories as $category) part, so that I can iterate as one would in JavaScript(
   ie, for var i etc, and then say some_array[i]). I tried to use -> to get the 
   $category as reference to the array iteration, but no luck. Thanks for willing
   to help!

Viewing 1 replies (of 1 total)

The topic ‘Nested loop: foreach category > foreach custom post in that category’
is closed to new replies.

## Tags

 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [abtx](https://wordpress.org/support/users/abtx/)
 * Last activity: [11 years, 8 months ago](https://wordpress.org/support/topic/foreach-slider/#post-5255078)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
