Quantcast
Channel: Rein Aris - Blog » Wordpress
Viewing all articles
Browse latest Browse all 10

Add ‘first’ and ‘last’ class to wp_list_pages

$
0
0

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');

Viewing all articles
Browse latest Browse all 10

Trending Articles