WordPress - Displaying Custom-Formatted Links To Page Children

This is just a quick and easy tip, but it was harder to find the answer to than I would have expected. (...or maybe I just didn't appease the Google Gods correctly when searching.)

If you have a page hierarchy within your WordPress site and you want to display (such as in the sidebar) the immediate children of the current page that you're on, here's the code to do it.

<?php
    // will display the subpages of this top level page
    $children = wp_list_pages("title_li=&child_of=" . $post->ID . "&echo=0");
    if ($children)
    {
        // custom formatting goes here, just print
        // $children when you want to render the link
        echo '<div id="pagelist">' . "\n";
        echo "<ul>\n$children\n</ul>\n";
        echo '</div> <!-- end div id="pagelist" -->' . "\n";
    } // end if test
?>

Please note that this method only lets you custom format the display around the actual link. wp_list_pages() will return the text with the anchor link already wrapped around it. I haven't dug into how to further format the link.

I hope that helps!

Category: 

Comments

This dissapoingtingly has nothing to do at all with a convenient way to call your children...

John, your comments read almost exactly like spammers. It took me a minute to realize that they were your comments.

...and another minute to get the joke. ;o)

Add new comment

You can optionally provide your email address, which will remain private, if you wish to be contacted directly about this comment.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.