How to find if a WordPress page has children

A friend called me yesterday* about a list of pages he was trying to set up. He needed pages with children indicated by a ">" after the link. "You'd think there'd be some kind of setting in wp_list_pages", he said, "but if there is, I can't see it."

The function we want is get_pages(), which passes the list of pages as an array to PHP to process. You can then test for the existence of children, and mark parent pages accordingly. This snippet of code picks up only top-level parent pages, and marks them with a ">" -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
//get our list of pages for PHP to process
$pages = get_pages('sort_column=menu_order'); 
//for each page in our list
  foreach ($pages as $page) {
//if it's not a child itself
        if (!$page->post_parent) {
//link the page
	echo "<li><a href=\"".get_page_link($page->ID)."\">".$page->post_title;
//see if it has children
        $children = wp_list_pages("title_li=&child_of=".$page->ID."&echo=0"); 
//if it does have children, put the arrow in
	if ($children) { echo " &gt; "; }
	echo "</a></li>";
  } 
}
  ?>

I must admit, it's a lot of code just for a couple of arrows, but if you're making - say - a navigation bar and want to indicate when fly-out menus are available, this is your boy.

If you want to exclude any particular page from the list, change line 8 to skip the page ID number you don't want:

1
if ((!$page->post_parent) && ($page->ID <> 999)) {

* I feel I want to add that people do call me about other stuff than WordPress. Sometimes ;-)

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Posted by Sue on September 13, 2009 in WordPress.

4 comments to "How to find if a WordPress page has children"

  1. matthew booth wrote:

    Wow... I have been looking for this information for a full day. Thanks!

  2. matthew booth wrote:

    actually, let me ask you this... I am trying to use wp_dropdown_pages() to create an html select dropdown of a certain pages children:

    wp_dropdown_pages('child_of=17&name=select_state&show_option_none=select a state')

    the only problem I am having is with the url. wp_dropdown_pages only uses page id as the option value:

    California

    Using javascript/jQuery I am trying to do a page redirect using the select's onchange event. For the life of me I can not figure out how to use wp_dropdown_pages() as a menu.

    Any ideas on how to create a select dropdown that will act as a menu? I was trying to figure out a way to use your code above to generate my own select dropdown and not use the built-in WP function.

  3. matthew booth wrote:

    nevermind *slaps forehead* it was on the get_pages() function reference page:

    <?php
    $pages = get_pages();
    foreach ($pages as $pagg) {
    $option = 'ID).'">';
    $option .= $pagg->post_title;
    $option .= '';
    echo $option;
    }
    ?>

  4. matthew booth wrote:

    <?php
    $pages = get_pages();
    foreach ($pages as $pagg) {
    $option = 'ID).'">';
    $option .= $pagg->post_title;
    $option .= '';
    echo $option;
    }
    ?>

Leave a Reply