Customise the selected header item in Wordpress

One issue I have come across recently in Wordpress is when you're on a specific page, be it the author page, or a custom post-type, it's very hard to change the selected menu item in the header, it is always the main posts page (in my case, usually blog).

So after a while I built a system that allows the ability of customising the current selected item. In short place the below code into your functions.php of your theme:

function sethead(,) {
	 = str_replace('a href=','a class="navitem" href=',);
	 = str_replace(' current_page_parent','',);
	 = explode("\n",);
	foreach( as ) {
		 = strpos(,'title="'..'"');
		//print ;
		if(===false) {
			 = ;
		}
		else {
			 = str_replace('"><a class="navitem"',' current_page_parent"><a class="navitem"',);
		}
		 .= "\n".;
	}
	return ;
}

Once you have done this, you can paste the below code in your header, in the same place you would usually put your navigation code (wp_list_pages())

 = wp_list_pages('sort_column=menu_order&echo=0&title_li=&depth=3'); 
switch (get_post_type()) {
	case 'special-content-type':
		 = sethead('Custom Area Name',);
	break;
	case 'custom-type':
		 = sethead('Another Area Name',);
	break;
	default:
		 = ;
	break;	
}
echo ;

Once this is done, you can then customise case by case what the selected navigation item should be, as you can see in the above example.

Hope this helps!