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

Related posts:

  1. Search from WordPress Admin (Post Listing) Redirecting to Home Page This is possibly the most bizarre thing I've ever seen....
  2. Delink Pages Plugin (Plugin and this post last updated: 10/20/2009 – Latest Plugin...
  3. Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...

Related posts brought to you by Yet Another Related Posts Plugin.

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)

Comment Form

Featured Sponsors


  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL
  • Jennifer: So I was testing this out to see if maybe something changed in the latest version - I had a test wordpress setup - and just dumped the mycomment funct
  • KDesign: Hi, thanks for the tutorial. I'm having problems though that I can't seem to find an answer to on the web. When I follow the example in the Codex h

About


Advertisements