WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to check if a parent Page has children. If it has children I would like to echo the children as a bulleted list with links to each child (basically a menu).

Right now I have the following code in loop-page.php and it checks if the Page has any children and returns an array with the children. I use an if statement to check if the number is greater than 0.

$children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);

if (count($children)) {
    // Echo children as a bulleted list of links

This part seems to work.

My question is how do I echo through the children as a bulleted list of links? Like the example below.

Let's say "Services" is the parent page and its children pages are "Grass Cutting", "Water Gardens", and "Landscaping".

count($children) would return 3 for the "Services" page since it has 3 children. Now it should echo them as so:

<li><a href="#">Grass Cutting</a></li>
<li><a href="#">Water Gardens</a></li>
<li><a href="#">Landscaping</a></li>

a href= should be the link to the page.

share|improve this question
up vote 2 down vote accepted

Well you just need to run a foreach loop over your $children but WordPress has a build in function for this already called wp list pages.

For example:

$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=1');

if ($children)
echo '<ul>' . $children . '</ul>';

There is a full reference and examples here: http://codex.wordpress.org/Function_Reference/wp_list_pages

share|improve this answer
I did consult the reference you linked before posting and understand the loop part but how do I echo the links in the loop? – user1822824 Feb 5 '13 at 6:45
The function will automatically create links and wrap the link in an <li>, you can change the output, have a look at the $args parameters. – Wyck Feb 5 '13 at 6:48
Sorry my example had echo=0, to have it return link you need to echo=1, I changed the above example. – Wyck Feb 5 '13 at 6:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.