Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahiirwa/3aee9cd7732b1f438dfcb5909075cc61 to your computer and use it in GitHub Desktop.
Save bahiirwa/3aee9cd7732b1f438dfcb5909075cc61 to your computer and use it in GitHub Desktop.
Accessing WordPress Children 4 levels deep. Please note in HTML I used boostrap. I also preserved the child page id for querying each level deep.
/*----------------------------------------------------------------------------------
Add this to functions php file to Return parent page ID or child as else.
------------------------------------------------------------------------------------*/
function get_top_parent_page_id() {
global $post;
if ($post->ancestors) {
return end($post->ancestors);
} else {
return $post->ID;
}
}
/*----------------------------------------------------------------------------------
Apply this in your page template to query levels deep
------------------------------------------------------------------------------------*/
<?php
$query = new WP_Query('pagename=page_slug'); while ( have_posts() ) : the_post(); ?>
<section class="blue-mid" id="blue-mid">
<div class="container">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="col-xs-12">
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<div class="col-xs-12">
<ul class="nav nav-tabs stress-tabs" role="tablist">
<?php while ( have_posts() ) : the_post(); $query = new WP_Query('pagename=page_slug');
if ($query->have_posts() ){ while ($query->have_posts() ) { $query->the_post(); } }
//Get children by calling function in functions.php for the top parent id
$current_page_id = get_top_parent_page_id();
$args = array( 'post_type' => 'page', 'post_parent' => $current_page_id, 'orderby' => 'title', 'order' => 'ASC' );
$child_query = new WP_Query($args);
$i = 0;
if ($child_query->have_posts() ){ while ($child_query->have_posts() ){ $child_query->the_post();
$i++; if( 1 == $i ) { ?>
<li role="presentation" class="active">
<a href="#<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" role="tab" data-toggle="tab"><?php the_title(); ?></a>
</li>
<?php } else { ?>
<li role="presentation">
<a href="#<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" role="tab" data-toggle="tab"><?php the_title(); ?></a>
</li>
<?php } } } wp_reset_postdata(); endwhile; ?>
</ul>
<!--
// Call the content when a particular tab is choosen
<?php while ( have_posts() ) : the_post(); $query = new WP_Query('pagename=page_slug');
if ($query->have_posts() ){ while ($query->have_posts() ) { $query->the_post(); } }
//Get children by calling function in functions.php for the top parent id
$current_page_id = get_top_parent_page_id();
$args = array( 'post_type' => 'page', 'post_parent' => $current_page_id, 'orderby' => 'title', 'order' => 'ASC' );
$child_query = new WP_Query($args);
$i = 0;
if ($child_query->have_posts() ){ while ($child_query->have_posts() ){ $child_query->the_post();
$i++; if( 1 == $i ) { ?>
<div class="tab-content">
<div id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" class="tab-pane fade in active">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php } else { ?>
<div class="tab-content">
<div id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" class="tab-pane fade in">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php } } } wp_reset_postdata(); endwhile; ?> -->
</div>
</div>
<div class="col-md-1"></div>
</div>
</div>
</section>
<section class="tab-mid" id="tab-mid">
<div class="container">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="col-xs-12">
<div class="pointed"></div>
<div class="tab-content">
<?php while ( have_posts() ) : the_post(); $query = new WP_Query('pagename=page_slug');
if ($query->have_posts() ){ while ($query->have_posts() ) { $query->the_post(); } }
//Get children by calling function in functions.php for the top parent id
$current_page_id = get_top_parent_page_id();
$args = array( 'post_type' => 'page', 'post_parent' => $current_page_id, 'orderby' => 'title', 'order' => 'ASC' );
$child_query = new WP_Query($args);
$i = 0;
if ($child_query->have_posts() ){
while ($child_query->have_posts() ){
$child_query->the_post();
//store the child ID to call the grandchildren later in loop
$grand_page_id = get_the_ID();
$i++; if( 1 == $i ) { ?>
<!--Start New Tab Content-->
<div id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" class="tab-pane fade in active">
<ol>
<?php
$grand_args = array( 'post_type' => 'page', 'post_parent' => $grand_page_id,
'orderby' => 'date','order' => 'ASC' );
$grand_child_query = new WP_Query($grand_args);
if ($grand_child_query->have_posts() ){
while ($grand_child_query->have_posts() ){
$grand_child_query->the_post();
$great_grand_page_id = get_the_ID();
?>
<li>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<ul>
<?php
$great_grand_args = array( 'post_type' => 'page', 'post_parent' => $great_grand_page_id,
'orderby' => 'title','order' => 'ASC' );
$great_grand_child_query = new WP_Query($great_grand_args);
if ($great_grand_child_query->have_posts() ){
while ($great_grand_child_query->have_posts() ){
$great_grand_child_query->the_post();
?>
<li><a data-toggle="modal" href="#<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>"><?php the_title(); ?></a></li>
<?php } } wp_reset_postdata(); ?>
</ul>
</li>
<?php
$great_grand_args = array( 'post_type' => 'page', 'post_parent' => $great_grand_page_id,
'orderby' => 'title','order' => 'ASC' );
$great_grand_child_query = new WP_Query($great_grand_args);
if ($great_grand_child_query->have_posts() ){
while ($great_grand_child_query->have_posts() ){
$great_grand_child_query->the_post();
?>
<div class="modal fade" id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" tabindex="-1" role="dialog" arialabelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<p class="title-staff"><?php the_title(); ?></p>
<p class="close" data-dismiss="modal" aria-hidden="true">&times;</p>
</div>
<div class="modal-body">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
<?php } } } } wp_reset_postdata(); ?>
</ol>
</div>
<!--End New Tab Content-->
<?php } else { ?>
<!--Start New Tab Content-->
<div id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" class="tab-pane fade in">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
&nbsp;
<ol>
<?php
$grand_args = array( 'post_type' => 'page', 'post_parent' => $grand_page_id,
'orderby' => 'title','order' => 'ASC' );
$grand_child_query = new WP_Query($grand_args);
if ($grand_child_query->have_posts() ){
while ($grand_child_query->have_posts() ){
$grand_child_query->the_post();
$great_grand_page_id = get_the_ID();
?>
<li>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<ul>
<?php
$great_grand_args = array( 'post_type' => 'page', 'post_parent' => $great_grand_page_id,
'orderby' => 'title','order' => 'ASC' );
$great_grand_child_query = new WP_Query($great_grand_args);
if ($great_grand_child_query->have_posts() ){
while ($great_grand_child_query->have_posts() ){
$great_grand_child_query->the_post();
?>
<li><a data-toggle="modal" href="#<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>"><?php the_title(); ?></a></li>
<?php } } wp_reset_postdata(); ?>
</ul>
</li>
<?php } } wp_reset_postdata(); ?>
<?php
$great_grand_args = array( 'post_type' => 'page', 'post_parent' => $great_grand_page_id,
'orderby' => 'title','order' => 'ASC' );
$great_grand_child_query = new WP_Query($great_grand_args);
if ($great_grand_child_query->have_posts() ){
while ($great_grand_child_query->have_posts() ){
$great_grand_child_query->the_post();
?>
<div class="modal fade" id="<?php echo $slug = get_post_field( 'post_name', get_post() ); ?>" tabindex="-1" role="dialog" arialabelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<p class="title-staff"><?php the_title(); ?></p>
<p class="close" data-dismiss="modal" aria-hidden="true">&times;</p>
</div>
<div class="modal-body">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
<?php } } wp_reset_postdata(); ?>
</ol>
</div>
<!--End New Tab Content-->
<?php } } } wp_reset_postdata(); endwhile; ?>
</div>
</div>
<div class="col-md-1"></div>
</div>
</div>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment