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 santanup789/ae7005f6c37e90d5bc1c072ba6a6189d to your computer and use it in GitHub Desktop.
Save santanup789/ae7005f6c37e90d5bc1c072ba6a6189d to your computer and use it in GitHub Desktop.
Get grand child pages, sub child pages along with siblings of parents of the current page using page depth calculation
<?php
global $post;
function get_all_subpages($page, $args = '', $output = OBJECT) {
// Validate 'page' parameter
if (! is_numeric($page))
$page = 0;
// Set up args
$default_args = array(
'post_type' => 'page',
);
if (empty($args))
$args = array();
elseif (! is_array($args))
if (is_string($args))
parse_str($args, $args);
else
$args = array();
$args = array_merge($default_args, $args);
$args['post_parent'] = $page;
// Validate 'output' parameter
$valid_output = array(OBJECT, ARRAY_A, ARRAY_N);
if (! in_array($output, $valid_output))
$output = OBJECT;
// Get children
$subpages = array();
$children = get_children($args, $output);
foreach ($children as $child) {
$subpages[] = $child;
if (OBJECT === $output)
$page = $child->ID;
elseif (ARRAY_A === $output)
$page = $child['ID'];
else
$page = $child[0];
// Get subpages by recursion
$subpages = array_merge($subpages, get_all_subpages($page, $args, $output));
}
return $subpages;
}
//print_r($all_current_subpages);
//$all_current_subpages = get_all_subpages(70);
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
);
$currentpage = $post->ID;
$parentpage = $post->post_parent;
$grandparent = get_post_ancestors($currentpage);
$grandparentID = $grandparent[1];
//echo $currentpage;
//echo $parentpage;
//echo $grandparentID;
$grand_child = get_all_subpages($grandparentID, $args);
$sub_child = get_all_subpages($parentpage, $args);
function get_current_page_depth(){
global $wp_query;
$object = $wp_query->get_queried_object();
$parent_id = $object->post_parent;
$depth = 0;
while($parent_id > 0){
$page = get_page($parent_id);
$parent_id = $page->post_parent;
$depth++;
}
return $depth;
}
//echo get_current_page_depth();
echo "<ul>";
if(get_current_page_depth() == 2) {
foreach ($sub_child as $value) {
//echo $value->post_title ."<br>";
if($post->ID != $value->ID){
$sss = get_permalink($value->ID);
echo "<li><a href='$sss' class='readMoreBtn style_2'>$value->post_title</a></li>";
}
}
}
if(get_current_page_depth() == 3) {
foreach ($grand_child as $value) {
//echo $value->post_title ."<br>";
if($post->ID != $value->ID){
$sss = get_permalink($value->ID);
echo "<li><a href='$sss' class='readMoreBtn style_2'>$value->post_title</a></li>";
}
}
}
echo "</ul>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment