Return to Snippet

Revision: 58995
at August 12, 2012 10:38 by SFLWebDev


Updated Code
add_filter('nav_menu_item_id','change_nav_menu_id',10,2);
function change_nav_menu_id($current_id,$item_details){
	global $menu_counter;
	
	$id_slug = strtolower(str_replace(' ', '-', $item_details->title));
	$id_slug = strtolower(str_replace('&', '',$id_slug ));
	$id_slug = strtolower(str_replace('--', '-',$id_slug ));

	return $id_slug;
}

Revision: 58994
at August 12, 2012 10:36 by SFLWebDev


Initial Code
add_filter('nav_menu_item_id','change_nav_menu_id',10,2);
function change_nav_menu_id($current_id,$item_details){
	global $menu_counter;
	
	$id_slug = strtolower(str_replace(' ', '-', $item_details->title));
	$id_slug = strtolower(str_replace('&', '',$class_slug ));
	$id_slug = strtolower(str_replace('--', '-',$class_slug ));

	return $id_slug;
}

Initial URL

                                

Initial Description
This function, when placed in a theme's 'functions.php' file will insert a custom id attribute into the '<li>' elements generated by a 'wp_nav_menu' function based on the title of the element. It will make a WordPress page or category into a hyphen separated string for CSS styling.

For example, if a post category has the name "This & That", it will generate an HTML id attribute as "this-that".

This version replaces spaces and ampersands - other characters can be added to the "str_replace" functions to filter out other characters that may exist in the "$item_details>title" property.

Initial Title
WordPress WP Nav Menu Custom HTML ID Based on Page or Category Titles

Initial Tags
wordpress

Initial Language
PHP