So with all this recent playing with wordpress that I’ve been up to, the more and more that I find my self saying ‘This was not designed for me was it?’ Sadly the answer tends to be ‘nope’, but it’s all in PHP so I can monkey with what ever I want to.
GOAL:
- respect the page order value
- nested list of links to pages for a nav
- system that allows me to define the HTML
- filter list down to only show pages where a custom feild ‘display’ = ‘nav’ (allows me to allow the page to decide where it shows up)
HOMEWORK:
- get page hierarchy() will not work, as it returns a flat list, thus looses context
- wp list pages() will not work as it does not allow for custom HTML and does not respect the page order stuff as written in the docs (weird, I know!)
- crap, I have to write my own
SOLUTION:
<?php
#should we be displaying things here?
function display_is ($p) {
return ( get_post_meta($p->ID,'display',true) == 'nav' );
}
#does the parent id match? (there has to be a better way then writing to globals?)
function ppid_is ($p) {
return ( $p->post_parent == $GLOBALS['DSP_current_post_parent'] );
}
#display the pages at this level (RECURSIVE)
function display_sub_pages ($pid, $class) {
$GLOBALS['DSP_current_post_parent'] = $pid;
$kids = get_posts( array(
'numberposts' => -1,
'post_parent' => $pid,
'post_type' => 'page',
'orderby' => 'menu_order',
'post_status' => 'publish',
) );
$kids = array_filter ( $kids, 'display_is' );
# this next one is retarded, but some how needed??? apparently get_posts(post_parent) doesn't work?
$kids = array_filter ( $kids, 'ppid_is' );
if (count($kids) > 0 ) {
printf( "\n <ul class=\'%s\'>\n", $class );
foreach ($kids as $p) {
printf(" <li class='%s' >\n", $p->post_title);
printf(" <a href='%s'>%s</a>\n", get_page_link($p->ID), $p->post_title );
display_sub_pages($p->ID, "_$class" );
print " </li>\n";
}
print " </ul>\n";
}
$GLOBALS['DSP_current_post_parent'] = '';
}
# kick start the process by starting at 0, then dig for sub-pages
display_sub_pages('0','nav');
?>
Now I can have my own HTML, I have a clean recursive call to build the entire tree (, the classes know how deep they are, in short it fits everything that I wished the default one had. It’s not the cleanest code I’ve done, my PHP-fu is rusty, sorry, apart from that, have at it kids. Swindle the code if you need something like this, comment if you found something broken.
Overall lessons learned:
- PHP callback’s sucks
- I always forget how stupid it is to just print HTML in your code untill I am forced to get back to it
- I’m not Wordpress’s target demographic, I guess it’s a good thing for the masses, but it kinda gets to me that there are not hooks for things that I think are simple and obvious.

