[wp-hackers] wp_list_pages

Adi Sieker ml at sieker.info
Fri Dec 10 18:05:18 UTC 2004


Hi,

attached is a preliminary version of wp_list_pages which supports
hierarchies. The file if a diff created with cvs diff -U3 filename

It supports the following options:
- child_of: Only select childern of the specified page ID. - default: 0
- optiondates: display the date the page was created. - default: 0
- modifieddate: if optiondates is set display the date the page was modified. - default: 0
- date_format: use this date format instead of the one from the options. - default: ''
- depth: only recurse this deep, if set to 0 display all levels. - default:0.
_ sort_column: Sort output by this column - default: post_title
- sort_order: specify sort order - default:ASC

Feedback welcome.

Adi

-- 
Adi J. Sieker                                   http://www.adsworth.info
Freelance developer
-------------- next part --------------
Index: template-functions-post.php
===================================================================
RCS file: /cvsroot/cafelog/wordpress/wp-includes/template-functions-post.php,v
retrieving revision 1.31
diff -U3 -r1.31 template-functions-post.php
--- template-functions-post.php	10 Oct 2004 18:02:29 -0000	1.31
+++ template-functions-post.php	10 Dec 2004 17:56:47 -0000
@@ -324,19 +324,82 @@
 	// TODO: Hierarchy.
 
 	parse_str($args, $r);
-	if (!isset($r['sort_column'])) $r['sort_column'] = 'title';
-	if (!isset($r['sort_order'])) $r['sort_order'] = 'asc';
+	if (!isset($r['child_of'])) $r['child_of'] = 0;
+	if (!isset($r['depth'])) $r['depth'] = 0;
+	if (!isset($r['optiondates'])) $r['optiondates'] = 0;
+	if (!isset($r['modifieddate'])) $r['modifieddate'] = 0;
+	if (!isset($r['sort_column'])) $r['sort_column'] = 'post_title';
+	if (!isset($r['sort_order'])) $r['sort_order'] = 'ASC';
 
-	$pages = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'static' ORDER BY post_" . $r['sort_column'] . " " . $r['sort_order'] = 'asc');
+	$exclusions = '';
+	if (!empty($r['exclude'])) {
+		$expages = preg_split('/[\s,]+/',$r['exclude']);
+		if (count($expages)) {
+			foreach ($expages as $expage) {
+				$exclusions .= ' AND ID <> ' . intval($expage) . ' ';
+			}
+		}
+	}
+	
+	$option_dates = '';
+	if(!empty($r['optiondates'])) {
+		if(!empty($r['modifieddate']))
+			$option_dates = ",UNIX_TIMESTAMP(post_modified) AS ts";
+		else 	
+			$option_dates = ",UNIX_TIMESTAMP(post_date) AS ts";
+	}
 
-	foreach ($pages as $page) {
-		echo '<li>';
+	$post_parent = '';
+	if($r['child_of']) {
+		$post_parent = ' AND post_parent=' . $r['child_of'] . ' '; 	
+	}
+	$pages = $wpdb->get_results("SELECT " .
+									"ID, post_title,post_parent " .
+									"$option_dates " .
+									"FROM $wpdb->posts " .
+									"WHERE post_status = 'static' " .
+									"$post_parent" .
+									"$exclusions " .
+									"ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
+	$page_tree = Array();
+	foreach($pages as $page) {
+		$page_tree[$page->ID]['title'] = $page->post_title;
+		
+		if(!empty($r['optiondates'])) {
+			$page_tree[$page->ID]['ts'] = $page->ts;
+		}		 	
+		$page_tree[$page->post_parent]['children'][] = $page->ID; 	
+	}
+	page_level_out($r['child_of'],$page_tree, $r);
+}
 
-		$title = apply_filters('the_title', $page->post_title);
+function page_level_out($parent, $page_tree, $args, $depth = 0) {
 
-		echo '<a href="' . get_page_link($page->ID) . '" title="' . htmlspecialchars($title) . '">' . $title . '</a>';
-		echo '</li>';
+	if($depth)
+		$indent = join(array_fill(0,$depth,"\t"));
+
+	foreach($page_tree[$parent]['children'] as $page_id) {
+		$cur_page = $page_tree[$page_id];
+		$title = $cur_page['title'];
+		echo $indent . '<li><a href="' . get_page_link($page_id) . '" title="' . htmlspecialchars($title) . '">' . $title . '</a>';
+		if(isset($cur_page['ts'])) {
+			$format = get_settings('date_format');
+			if(isset($args['date_format']))
+				$format = $args['date_format'];
+			echo " " . gmdate($format,$cur_page['ts']);
+		}
+		echo "\n";
+
+		if(isset($cur_page['children']) && is_array($cur_page['children'])) {
+			echo "$indent<ul>\n";
+			$new_depth = $depth + 1;
+
+			if(!$args['depth'] || $depth < ($args['depth']-1)) {
+				page_level_out($page_id,$page_tree, $args, $new_depth);
+			}
+			echo "$indent</ul>\n";
+		}
+		echo "$indent</li>\n"; 
 	}
 }
-
 ?>
\ No newline at end of file


More information about the hackers mailing list