Skip to content

Instantly share code, notes, and snippets.

@wvega
Created August 13, 2011 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wvega/1144247 to your computer and use it in GitHub Desktop.
Save wvega/1144247 to your computer and use it in GitHub Desktop.
Add a subpages menu to WordPress pages
<?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 }
}
@braddalton
Copy link

Why do you use global $post; when using get_the_ID() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment