List subpages of a parent page

I’m using the wp_list_pages function a lot for displaying parent pages in the main menu.

I usually only show the parent pages without children (on a homepage). In the function I use depth=1 to only sum up the top level pages:

wp_list_pages('title_li=&depth=1' );

When visiting a page i like to sum up the subpages of the parent page in a submenu. Sometimes (when using a submenu on a left sidebar) i like to show the parent page again on the top of the subitems. I created a function for this which you can put in your functions.php template file. This function is only created to list 1 level of subpages, no sub-sub is supported (yet) on the moment. Maybe I will update the function when I need it myself :)

Continue reading

Add ‘first’ and ‘last’ class to wp_list_pages

As a frontend developer I like to have a lot of pre-defined classes in page lists, menu’s and things like that. Unfortunately WordPress doesn’t have a first and last class in the wp_list_pages function. I think this should be there by default!

Based on this piece of code (thanks Robert) I created a hook you can put in your functions.php template file. So the generated page list will get first and last classes, good luck!

/* last and first classes on wp_list_pages */
function wp_list_pages_firstlast($pageList) {
 
	$pageList = preg_replace('/class="/','class="first ', $pageList, 1);
 
	$reversedString = strrev($pageList);
	$reversedSearch = '/'.strrev('class="').'/';
	$endClass = strrev('class="last ');
 
	$pageList = strrev(preg_replace($reversedSearch,$endClass, $reversedString, 1));
 
	return $pageList;
}
add_filter('wp_list_pages', 'wp_list_pages_firstlast');