Home » Development » WordPress » Sub-page Count » Version 0.3

Someone pointed out to me a nice feature to have. That is, if a page has no subpages, then don’t simply display “0,” just display nothing. I agree, as it’s more æsthetically pleasing.

So here it is:

<?php
$children = dvl_wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0&depth=1&sort_order=asc');
if ($children) { ?>
<ul>
<?php
preg_match_all('#<li class="page_item page-item-(.*?)">#msi', $children, $childrenIDs);
 
foreach($childrenIDs[1] as &$childID){
    $count   = count(explode("\n", dvl_wp_list_pages('title_li=&child_of='.$childID.'&echo=0&depth=1&sort_order=desc'))) - 1;
    $match[] = '#<li class="page_item page-item-'.$childID.'"><a href="(.*?)" title="(.*?)">(.*?)</a></li>#msi';
 
    if(intval($count) !== 0) $replace[] = "\t".'<li class="page_item page-item-'.$childID.'"><a href="\\1" title="\\2">\\3 ('.$count.')</a></li>';
    else $replace[] = "\t".'<li class="page_item page-item-'.$childID.'"><a href="\\1" title="\\2">\\3</a></li>';
}
echo preg_replace($match, $replace, $children);
?>
</ul>
<?php
}
?>

This is compatible with WordPress 2.4-2.9.2 (3.0 is also supported)

Respond

You must be logged in to post a comment.