Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ancillaryfactory/2043500 to your computer and use it in GitHub Desktop.
Save ancillaryfactory/2043500 to your computer and use it in GitHub Desktop.
Wordpress: Add a subpages menu
<?php
function subpages() {
global $post;
// Output a link to the parent page, if we are on a subpage
if (!empty($post->post_parent)) {
$title = esc_attr(get_the_title($post->post_parent)) ?>
<p><b>Parent page:</b><br />
<a href="<?php echo get_permalink($post->post_parent); ?>" title="<?php echo esc_attr($title); ?>"><?php echo $title ?></a></p>
<?php }
// Output a menu of subpages only if current page has any subpages.
$params = array('child_of' => get_the_ID(), 'title_li' => null);
// You may want to add a title here. Something like "Table of Contents" or
// "Subpages".
if (count(get_pages($params))) { ?>
<ul class="entry-toc"><?php wp_list_pages($params) ?></ul>
<?php }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment