loop inside loop
-
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
-
what theme are you using?
are you planning to build a custom page template?
what exactly do you want to display on the page?
my own theme
start by creating a page template https://codex.www.ads-software.com/Page_Templates, possibly based on page.php of your theme.
https://codex.www.ads-software.com/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.
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
for any shortcode, you need to
'return'
the results instead of'echo'
ing them; https://codex.www.ads-software.com/Shortcode_APIbut 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?
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 contentthanks for giving wonderful replay
- The topic ‘loop inside loop’ is closed to new replies.