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