Title: loop inside loop
Last modified: August 22, 2016

---

# loop inside loop

 *  [dileepraja](https://wordpress.org/support/users/dileepraja/)
 * (@dileepraja)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/)
 * Hi using wp_query how to get the parent and child pages loop
    it shuold work 
   all times parent all the child pages content should be displayed

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860225)
 * what theme are you using?
 * are you planning to build a custom page template?
 * what exactly do you want to display on the page?
 *  Thread Starter [dileepraja](https://wordpress.org/support/users/dileepraja/)
 * (@dileepraja)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860229)
 * my own theme
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860255)
 * start by creating a page template [http://codex.wordpress.org/Page_Templates](http://codex.wordpress.org/Page_Templates),
   possibly based on page.php of your theme.
 * [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * then replace the normal loop with this general structure to get all top level
   pages and then loop through their direct children:
 *     ```
       <?php
       $top_level_args = array(
         'post_type' => 'page',
         'post_parent' => 0,
         'posts_per_page' => -1
       );
       $top_level = new WP_Query( $top_level_args );
       if( $top_level->have_posts() ) :
   
       	while( $top_level->have_posts() ) :
       	$top_level->the_post();
   
       	the_title(); //top level parent page title - add your own HTML for formatting//
   
       	//start inner loop//
   
       	$parent_page_title = $post->post_title;
       	$child_level_args = array(
       	  'post_type' => 'page',
       	  'post_parent' => $post->ID,
       	  'posts_per_page' => -1
       	);
       	$child_level = new WP_Query( $child_level_args );
       	if( $child_level->have_posts() ) :
   
       		while( $child_level->have_posts() ) :
       		$child_level->the_post();
   
       		the_title(); //child level page title - add your own HTML for formatting//
       		the_content(); //child level page content - add your own HTML for formatting//
   
       		endwhile;
   
       		else: echo 'no child page(s) for \'' . $parent_page_title . '\'';
       	endif; wp_reset_postdata();
   
       //end of inner loop//
   
       endwhile;
   
       else: echo 'no top level pages';
   
       endif; wp_reset_postdata();
   
       ?>
       ```
   
 * if you also need child pages from lower levels, you need to adapt the code, for
   example by using `'child_of'` instead of
 * > ‘parent’
 *  in the inner loop.
 *  Thread Starter [dileepraja](https://wordpress.org/support/users/dileepraja/)
 * (@dileepraja)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860261)
 * function shortcode_parent(){
    $args = array(‘post_type’ => ‘vh_accommodation’,‘
   orderby’ => ‘title’, ‘order’ => ‘ASC’, ‘post_parent’ => 0, ‘posts_per_page’=>’-
   1′); $the_query1 = new WP_Query($args); echo ‘<h2 style=”background: none repeat
   scroll 0 0 #dba63f; color: #fff; font-family: Open Sans; font-size: 18px; margin-
   bottom: 15px; margin-top: 0 !important; padding: 10px; border-bottom: 2px solid#
   c18c25;”>’; echo “Rooms Available”; echo ‘</h2>’;
 *  if($the_query1->have_posts()){
    while ($the_query1->have_posts()) { $the_query1-
   >the_post(); global $thisPost;
 *  $parent_page_title = $post->post_title;
    //$thisPost->post_parent; //echo $thisPost;
 *  $inner_args = array(‘post_type’ => ‘vh_accommodation’, ‘post_parent’ => $post-
   >ID, ‘posts_per_page’ => 5,’orderby’ => ‘title’, ‘order’ => ‘ASC’,);
    $inner_query
   = new WP_Query($inner_args);
 *  if($inner_query->have_posts()){
    while ($inner_query->have_posts()) { $inner_query-
   >the_post(); //echo the_title(); ?> <div class=”col-md-6″ style=”padding-left:
   0;padding-right:0;”> <div class=”room_img1 image-panel” style=”margin-bottom:
   25px;padding:15px;”> <?php if(has_post_thumbnail()) ?> <?php echo the_post_thumbnail(
   array(350,250));?> <img src=”<?php echo get_template_directory_uri().’/images/
   6.jpg’?>” style=”width:350px;height:250px;”>
 *  <p class=”text-center page-heading”><?php the_title();?></p>
    <p><?php the_excerpt();?
   ></p> <?php if(get_post_meta(get_the_ID(),’room_size’,true) || get_post_meta(
   get_the_ID(),’room_bed’,true) || get_post_meta(get_the_ID(),’max_person’,true))?
   > <p class=”room_exrt”><?php if(get_post_meta(get_the_ID(),’max_person’,true)){?
   ><div class=”room_exrt”><span class=”r-title text-center” style=”padding-left:
   118px;”><?php _e(‘Max person : ‘); ?></span><?php $person=get_post_meta(get_the_ID(),’
   max_person’,true); echo “”;for($i=1;$i<=$person;$i++){echo “<span class=’icon-
   male’></span>”;}?></div><?php }?></span> <span class=”room_exrt”><?php if(get_post_meta(
   get_the_ID(),’room_bed’,true)){?><span><span class=”r-title” style=”padding-left:
   118px;”><?php _e(‘Beds : ‘);?></span><?php echo get_post_meta(get_the_ID(),’room_bed’,
   true);?></span><?php }?></p> <p class=”room_exrt”><?php if(get_post_meta(get_the_ID(),’
   room_size’,true)){?><span><?php echo get_post_meta(get_the_ID(),’room_size’,true);?
   ></span><?php }?></p> <p class=”text-center” style=”padding-top:5px;”><span class
   =”icon-food”></span> <span class=”icon-desktop”></span> <span class=”icon-glass”
   ></span> <span class=”icon-globe”></span> <span class=”icon-credit-card”></span
   ></p> <p class=”text-center”>” style=”color:#D4B66D !important;text-decoration:
   none;”>Read More</p>
 *  </br/>
 *  </div>
    </div> <?php } } } }wp_reset_postdata(); } add_shortcode(‘accommodation1′,’
   shortcode_parent’);
 * i wrote this codeing but i am running all parent pages all sub pages will be 
   displaying
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860266)
 * for any shortcode, you need to `'return'` the results instead of `'echo'`ing 
   them; [http://codex.wordpress.org/Shortcode_API](http://codex.wordpress.org/Shortcode_API)
 * > but i am running all parent pages all sub pages will be displaying
 * what is wrong with the output? what exactly do you want to show?
 *  Thread Starter [dileepraja](https://wordpress.org/support/users/dileepraja/)
 * (@dileepraja)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860330)
 * hi i created
    Home Page subpage1 subpage2 subpage3 Main Page MainSubpage1 MainSubpage2
   MainSubpage3 i need display the content of subpages in Home page/Main Page but
   i am writing the loop both the pages displaying same content
 * thanks for giving wonderful replay

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

The topic ‘loop inside loop’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [dileepraja](https://wordpress.org/support/users/dileepraja/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/loop-inside-loop-1/#post-5860330)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
