WordPress wp_nav_menu output class when menu item has children

Posted in Web Devel by admin on the September 9th, 2011

I recently wanted to insert a class for the case where a menu item has children in WordPress. There doesn’t appear to be any option to do this in wp_nav_menu function, so I started doing some research.

I found out from this article how to add a ‘last-menu-item’ css class.

This doesn’t quite work for what I wanted though, because the returned data object from WordPress doesn’t tell you if the current node has children.

So, I used a walker object instead (there’s a good article on their usage here) and then used the theory behind the first article to achieve my objective. Nice.

<?php
class TopnavWalker extends Walker_Nav_Menu
{
	function start_lvl(&$output, $depth) {
		$indent = str_repeat("\t", $depth);
		$intPos = strripos($output,'menu-item');
		$output = sprintf("%s has-children %s",
			substr($output,0,$intPos),
			substr($output,$intPos,strlen($output))
		);
		$output .= "\n$indent<ul class=\"sub-menu\">\n";
	}
}
?>

Tour De Picnic

Posted in Uncategorized by admin on the September 2nd, 2011

Ronan Murphy, Dave Clarke, David Byrne and I recently put a site live for Tour De Picnic. This is a charity cycle to the Electric Picnic festival in Stradbally in aid of Temple Street Hospital. Ronan was doing the cycle and we decided it would be nice to track him on his journey.

Thus was born a website that used an iphone, a handy iPhone app called SendLocation, a little lamp coding, google maps api and some html/css/javascript to update Ronans location throughout the trip. We also integrated a twitter feed that updated with tweets using the #tourdepicnic tag.

The results can be seen at http://www.ronanmurphy.ie/tourdepicnic/