A Smarter Menu for WordPress

With the release of wordpress 3.0 this function is no longer needed, I’d like to thank everyone showing interest in it:) I’ve always thought it would be nice to combine pages and categories all inside one menu, and recently I was looking for a nice way to add subtitles as well. So I thought it would be cool to write my own menu function to suit my own needs.First off I want to give credit to a couple of posts on how to add subtitles via custom fields, sadly, each had their separate issue.

In the first post I found by Shantanu you would have to hack classes.php, a WordPress core file (not my cup of tea). The second one by Theme Shaper was lacking one very essential thing, dynamic active classes! (A non-dynamic menu is a non-happy menu).

The second post by Theme Shaper was almost doing what I was looking for so I decided to hack on it. The code was wrapped in a function which I could easily add to any of my themes, and it uses custom fields for the job, easy-peasy:)

I’ve spent a great deal of time figuring out how to do this and finally I came up with a solution that worked, next phase was to add categories and home link, ontop of that, I’ve added some more snacks, for instance you can chose if you want the home link and categories to show up, (with active classes of course) and you can sort the listing. Subtitles for home and categories are fetched from their respective description container. Enjoy!

New code as of 28.02.10 :) Put wp_smart_menu() where you want your menu to appear and paste the code below inside your theme’s functions.php file. If you feel it is missing functionality please add your comment below. Otherwise, enjoy!

// Smart Menu with Subtitles by Jon Kristian Nilsen - http://jonkristian.no
function wp_smart_menu() {
	// Some basic settings
	$showhome = "true"; // Show home link true/false.
	$homedesc = get_bloginfo('description'); // Subtitle for the home url.
	$showcat = "true"; // Show categories true/false.
	$showpage = "true"; // Show pages true/false.
	$subtitle = "subtitle"; // The meta key for your subtitle text on pages.

echo '<ul id="menu">';

	// Display Home link
	if ($showhome == "true") {
		$homeurl = get_bloginfo('home');
		echo '<li class="item home'.((is_front_page()) ? ' active' : '').'">';
		echo '<span class="title"><a href="'.$homeurl.'" title="Home">Home</a></span>';
		echo '<span class="subtitle"><p>'.$homedesc.'</p></span>';
		echo '</li>';
	}

	// Build pages
	if ($showpage == "true") {
	$pages = get_pages();
	global $post;

		foreach ($pages as $page) {
		$pageurl = get_permalink($page->ID);
		$subtitle = get_post_meta($page->ID, 'subtitle', true);

		echo '<li class="item '.$page->post_name.((is_page($page->ID)) ? ' active' : '').'">';
			if ($page->post_parent != 0) {
				echo '<ul class="sub"><li>';
				echo '<span class="title"><a href="'.$pageurl.'" title="'.$page->post_title.'">'.$page->post_title.'</a></span>';
				echo '<span class="subtitle"><p>'.$subtitle.'</p></span>';
				echo '</li></ul>';
			} else {
				echo '<span class="title"><a href="'.$pageurl.'" title="'.$page->post_title.'">'.$page->post_title.'</a></span>';
				echo '<span class="subtitle"><p>'.$subtitle.'</p></span>';
			}
		echo '</li>';
		}
	}

	// Build categories.
	if ($showcat == "true") {
	$categories = get_categories();
	global $wp_query;

		foreach ($categories as $cat) {
		$cat_url = get_category_link($cat->term_id);

		echo '<li class="item '.$cat->slug.((is_category($cat->term_id) || is_single() && in_category($cat->term_id,$wp_query->post->ID)) ? ' active' : '').'">';
			if ($cat->parent != 0) {
				echo '<ul class="sub"><li>';
				echo '<span class="title"><a href="'.$cat_url.'" title="'.$cat->cat_name.'">'.$cat->cat_name.'</a></span>';
				echo '<span class="subtitle"><p>'.$cat->description.'</p></span>';
				echo '</li></ul>';
			} else {
				echo '<span class="title"><a href="'.$cat_url.'" title="'.$cat->cat_name.'">'.$cat->cat_name.'</a></span>';
				echo '<span class="subtitle"><p>'.$cat->description.'</p></span>';
			}
		echo '</li>';
		}
	}

echo '</ul>';

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>