scriptygoddess

30 Apr, 2007

Listing subpages in WordPress

Posted by: Jennifer In: WordPress|WordPress Bookmarks|WordPress Hacks

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; ?>

6 Responses to "Listing subpages in WordPress"

1 | Josh

June 29th, 2007 at 10:23 pm

Avatar

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

July 4th, 2007 at 8:15 pm

Avatar

(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

January 17th, 2008 at 12:55 pm

Avatar

[…] 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 […]

4 | tom

September 12th, 2008 at 12:44 pm

Avatar

phew!

Works perfectly, tried about 20 other solutions but none quite did what they said they would do.

Many thanks

5 | Diether

February 8th, 2009 at 2:37 am

Avatar

Hi , Im trying to add this, but it doesnt seem to work.
Where exactly in my header.php do I need to add this ?

6 | Jennifer

February 8th, 2009 at 8:08 am

Avatar

@diether
There have been some changes to wordpress since this came out. I had another way of doing this posted here:
http://www.scriptygoddess.com/archives/2008/01/17/using-wordpress-with-some-modifications-as-a-cms/
(look at the first block of code in step 3 – the text and code that follows in italics is outdated stuff)

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements