Created
May 20, 2021 08:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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