Listing subpages in Wordpress

I was looking for a way to list sub pages in a navigation bar if you were either on the “parent” page or one of it’s sub pages. I found this page on Clioweb that was perfect.

What’s also nice is that it gives you the variable $parent_id to play with if you need to. Copied from his site:

<?php
$page = $wp_query->post;
$parent_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE ID ='$page->post_parent;'");
if(!$parent_id) $parent_id = $post->ID;
?>

Then, to actually generate the HTML for the page list, I use the following code in the body of my page template:

<?php if(wp_list_pages("child_of=".$parent_id."&echo=0")): ?>
<ul id="subnav">
<?php wp_list_pages("title_li=&child_of=".$parent_id."&sort_column=menu_order&depth=1");?>
</ul>
<?php endif; ?>

3 Responses to “Listing subpages in Wordpress”

  1. Josh Says:

    Trying to get this to work on my site but of course i’m having issues. Could you spare some knowledge as to exactly where you put the two pieces of code? Thanks

  2. Jennifer Says:

    (Sorry I’m getting back to this so late - apparently a bunch of comments never got emailed to me, so they’ve been stuck in moderation)

    Anyway…The first snippet I put in header.php above any HTML.

    Then the second snippet is in my sidebar. HTH

  3. scriptygoddess » Blog Archive » Using Wordpress (with some modifications) as a CMS Says:

    [...] any case, this is how I did the subnavigation. Using a trick I’d posted about before - I get the list of subpages this way: (This goes at the very top of my header.php template [...]

Leave a Reply

Subscribe without commenting