Skip to content

Instantly share code, notes, and snippets.

@gstricklind
Last active April 24, 2019 03:13
Show Gist options
  • Save gstricklind/27301cb225f54d57ff4d to your computer and use it in GitHub Desktop.
Save gstricklind/27301cb225f54d57ff4d to your computer and use it in GitHub Desktop.
WordPress: Dynamically shows first level child pages of a parent page without using a widget or menu.
<?php
global $post;
if(!$post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
} else{
if($post->ancestors) {
//$ancestors = end($post->ancestors);
$ancestors = end( get_post_ancestors($post) );
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) {
?>
<aside id="child-pages" class="widget widget_child_pages">
<ul>
<?php echo $children; ?>
</ul>
</aside>
<?php } ?>
<? //Placement: I usually place this code above the normal call for the sidebar ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment