Get current post ancestors and list it’s children in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
global $post; $ancestor = get_post_ancestors($post->ID); $id = ($ancestor) ? $ancestor[count($ancestor)-1]: $post->ID; $post_ancestor = get_post($id); $args = array( 'post_type' => 'page', 'post_parent' => $id, 'posts_per_page' => -1 ); $pages = get_posts($args); if($pages){ echo '<div class="subpages">'; echo '<h3>' . $post_ancestor->post_title . '</h3>'; echo '<ul>'; foreach($pages as $page){ if($page->ID === $post->ID){ $class = 'active'; } else { $class = ''; } echo '<li class="' . $class . '"><a href="' . get_permalink($page->ID) . '">' . $page->post_title . '</a></li>'; } echo '</ul>'; echo '</div>'; } |