Blog

WordPress: list pages include tree function

Wordpress functions

wordpress-logo-hoz-rgb

I continue to use WordPress as my primary choice for a website CMS. Occasionally, a website’s needs exceed the core functionality. Luckily it’s core functions are written with PHP so extending them is an easy job. I’m going to list some custom functions I’ve created on this blog to help you fellow WordPressers out there.

Today I needed use the ‘wp_list_pages’ function, but to only list the tree (ie. itself and it’s children) of a certain post. The existing function has arguments for ‘include’, ‘exclude’, ‘exclude_tree’ – but ‘include_tree’ is missing. Therefore I created my own function to do this.

Usage:

Place this inside /wp-content/themes/<theme folder>/functions.php:

<?php

function js_list_pages_include_tree($args){
$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',
'link_before' => '', 'link_after' => '',
'include_tree' => 0
);

$r = wp_parse_args( $args, $defaults );

// Print the parent page
$pages = get_pages('title_li=&include='.$r['include_tree'].'&depth=0');

// Print the children
foreach ($pages as $pagg){
echo '<li><a href="'.get_page_link($pagg->ID).'">'. $pagg->post_title.'</a>';
echo "<ul>";
wp_list_pages('title_li=&child_of='.$r['include_tree'].'&depth='.($r['depth']-1));
echo "</ul>";
echo '</li>';
}

}

?>

And place this inside your template PHP (amending the ‘include_tree’ argument for the post tree you wish to display:

<?php js_list_pages_include_tree('include_tree=10'); ?>

Leave me a comment if you find any bugs.

(incidently, this post uses the SyntaxHighlighter Evolved plugin to display the pretty PHP code)


7 comments:


  1. Thank you! I was needing badly something as include_tree. This works perfectly! ;o]

    Cheers

    Guto says:

  2. Hi!
    Thanks for this script! Very useful!

    By the way! You should replace $r['include_tree'] to $pagg->ID in the 23th line because it lists the subpages of the first page to every other pages if you include multiple parent pages.

    scidar says:

  3. Tried scidars suggestion and it works fine.

    I want to remove indent and change sort order to page order, replaced

    menu_order, post_title

    with

    menu_order&title_li=&depth=-1

    in functions.php but didn’t work – can you point out where to edit this file to achieve a list without indent sorted by page order

    neil says:

  4. Thanks for a great script. This really fills a hole in the wordpress menus.

    My only caution is that I’m using “All in One SEO” plugin and this script ignores the “menu label” All in One assigns for the parent of the tree. Not the children, just the parent.

    Other than that, its great!!!

    OKParrothead says:

  5. Any chance you could add current_page_item class functionality ? :-)

    Gwilym says:

  6. Great start, although I’ve found an alternative that includes the current_page_item and _ancestor class on the first page.

    http://michaeldozark.wordpress.com/2010/05/12/better-includes-and-excludes-for-wordpress/

    Martin Lucas says:

  7. Works like a charm sir! thank you

    Bryan Acerden says:


Leave a comment