Nov 21, 2010
My Digital Life Editorial Team

List Child of a Parent Page in WordPress (Fix wp_list_pages child_of Not Working)

wp_list_pages() template tag in WordPress is used to display a list of WordPress Pages as links, commonly in Sidebar or Header. One of the parameters of wp_list_pages() is child_of, which supposedly restrict and force the tag to display the sub-pages (including direct descendants and recurring grandchildren) of a single Page only based on the ID integer value of the Page specified, where the value 0 (default) displays all Pages.

When user uses the wp_list_pages() array function with child_of parameter, such as “wp_list_pages(“child_of=7″);”, no Pages’ links are been returned. The HTML code which supposed to populated with links to various WordPress Pages which are has the Page’s ID specified by child_of switch is empty and blank.

When child_of parameter is removed, the wp_list_pages() template tag works properly as per normal. Of course, without child_of option, it returns all Pages available.

For unknown reason, the child_of parameter of wp_list_pages() is broken and causes the wp_list_pages() function to be not working. As a workaround, user can use get_pages() function instead, although the code will be longer as a loop is needed to process all Pages returned in the result.

Example code:

<?php

$pages = get_pages ('child_of=8');

foreach ($pages as $page)
{
	echo '<li><a href="' . get_page_link($page->ID) . '">' .
	$page->post_title . '</a></li>';
}

?>

get_pages() function has an advantage that it accept “parant” parameter, which only display Pages that have the parent Page with ID as the value specified. Together with child_of parameter, parent parameter can be used to limit the ‘depth’ of the child_of parameter, so only one generation of descendants can be retrieved. Both child_of and parent parameter should have the same ID to achieve the effect.

Example Code:

<?php

$pages = get_pages ('child_of=10&parent=10');

foreach ($pages as $page)
{
?>
	<li><a href="<?php echo get_page_link($page->ID) ?>"><?php
	echo $page->post_title ?></a></li>
<?php
}
?>

Related posts:

  1. Paginating or Split WordPress Post or Page with NextPage in WordPress Not Working
  2. How to Customize, Modify or Change WordPress Database Connection Error Page
  3. How to List and Show WordPress Posts That Comments and Pings Off (Not Allow)
  4. Adding Extra More Fields to WordPress Write/Edit Post/Page Right Column
  5. Add and Put AdSense Ads Code and JavaScript to Google Page Creator Website

1 Comment

  • Thanks I needed this, very helpful :-)

Leave a comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Subscribe

Free email subscriptions
Get latest updates in email for free:

Translate This Page