Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Luke Janicke on "Category ID/slug from wp_get_nav_menu_items"

$
0
0

I’m writing a custom "custom menu" function. The menu items are (mostly) categories. I want to add the category slug as a class to the li elements.

function news_custom_menu( $location ) {

	// Custom Menu

	if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $location ] ) ) {
		printf( '<ul class="%s">' . "\n", $location );
		$items = wp_get_nav_menu_items( $locations[ $location ] );
		foreach ( (array) $items as $key => $item ) {
			$title = $item->title;
			$url = $item->url;
			$ID = $item->ID;
			$category = get_category( $ID );
			$slug = $category->slug;
			printf ( '<li class="%s"><a href="%s">%s</a></li>' . "\n", $slug, $url, $title );
		}
		echo '</ul>';
	}

$title and $url work. However, either $ID is not the category ID or I’m doing something wrong with get_category().

Which item in the returned object ($item) is the category ID?

Reference:
http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items


Viewing all articles
Browse latest Browse all 8245

Trending Articles