"Change wp_list_pages function in wordpress so that it returns the data"

Date:February 5th, 2010
Comments: 7
  • Join date: 11-30-99
  • Posts: 23
View Counter:
Rate it
  • How would you prefer to migrate to new technology?

    View Results

    Loading ... Loading ...
  • bodytext bodytext bodytext

wp_list_pages function returns a “UL” list. This formatting cannot be changed, at least in my version of wordpress.What I want is to be able to *RECURSIVELY* retrieve pages and be able to use any format I choose on the result.That is why I copied an original wp_list_pages function, located in wp-includes/post-template.php.
I created my version of this function, which works the same way but instead formatting things just gives the data back.
I called it wp_get_pages:

function wp_get_pages($args = '') {
	if ( is_array($args) )
		$r =  &$args;
		parse_str($args, $r);

	$defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_option('date_format'),
		'child_of' => 0, 'exclude' => "", 'title_li' => __('Pages'), 'echo' => 1, 'authors' => '', 'sort_column' => 'menu_order, post_title');
	$r = array_merge($defaults, $r);

	$output = '';
	$current_page = 0;

	// sanitize, mostly to keep spaces out
	$r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);

	// Allow plugins to filter an array of excluded pages
	$r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));

	// Query pages.
	$pages = get_pages($r);

	return $pages;

Say, you want to get all children pages of parent and process them instead of printing out.

It can be done like this:

// set parent id
define $parent_id = 10;

	  'child_of' => $parent_id

	$pageposts = wp_get_pages($args);
	foreach ($pageposts as $post):
		// WordPress loop setup complete, use $post variable here

Be Sociable, Share!
Interesting entry?
VN:F [1.8.5_1061]
Rating: -1 (from 1 vote)
0 votes 'YES'  1 votes 'NO'



Be Sociable, Share!



7 Responses to “Change wp_list_pages function in wordpress so that it returns the data”

  1. pable says:

    How can I make  it to display the posts like a tree (hierarchy)?

    • admin says:

      wp_list_pages supposed to return tree by default, and you can control how this tree will look like.

      In the http://codex.wordpress.org/Function_Reference/wp_list_pages there is a description of what exactly will be returned.

      Like :

      By default, wp_list_pages() generates a nested, unordered list of WordPress Pages created with the Write > Page admin panel. You can remove the outermost item (li.pagenav) and list (ul) by setting the title_li parameter to an empty string.

      All list items (li) generated by wp_list_pages() are marked with the class page_item. When wp_list_pages() is called while displaying a Page, the list item for that Page is given the additional class current_page_item.

      Use CSS to display the posts like tree of any kind:

      .pagenav { … } /* the outermost list item; contains whole list */
      .page-item-2 { … } /* item for Page ID 2 */
      .page_item { … } /* any Page item */
      .current_page_item { … } /* the current Page */
      .current_page_parent { … } /* parent of the current Page */
      .current_page_ancestor { … } /* any ancestor of the current Page */
  2. kennedy says:

    Thanks so much for this article. My question is how can i display the children under age page. I would like to have this format:
    where OM OSS is the page title and then under them are their children.
    I will really appreciate if anyone can help me out.

  3. admin says:

    Try this. Put the code here in an empty test.html file. Open with firefox (i tested with 21.0 . Should look similar to what you want.

    More on such CSS styling is here :


    .pagenav {
    list-style-type: none;

    .pagenav ul {
    list-style-type: none;
    position: relative;
    display: inline-table;


    .pagenav ul:after {
    content: "";
    clear: both;
    display: block;

    .pagenav ul li {
    float: left;margin-left:30px;

    .pagenav ul ul {
    padding: 0;
    position: absolute; top: 100%;


    .pagenav ul ul li {
    float: none;
    margin: 0;
    position: relative;


    <li class="pagenav">

       <li class="page-item-2 page_item ">
       OM OSS 2
           <li class="page-item-21 page_item ">

    Link 21

           <li class="page-item-22 page_item ">

    Link 22

       <li class="page-item-3 page_item">
       OM OSS 3
           <li class="page-item-31 page_item ">

    Link 31

           <li class="page-item-32 page_item ">

    Link 32

       <li class="page-item-4 page_item">
       OM OSS 4
           <li class="page-item-41 page_item ">

    Link 41

           <li class="page-item-42 page_item ">

    Link 42