How to make “Home” link visible on all pages except the front page in wordpress

Once I needed to do this, but could not find how to do it without editing a code. So by some similar examples I made this code.

Place this in your themes directory, functions.php:

add_filter('wp_get_nav_menu_items', 'removeHome');
function removeHome($menu) 
{
	$homeUrl = get_bloginfo('url') . '/';
	if (is_home()) {	
		foreach ($menu as $key => &$m) {
 
			if ($m->url == $homeUrl) {
				unset($menu[$key]);
				break;
			}
		}
	}
	return $menu;
}

Here we assume that home link has ‘/’ at the end. But if you want that it would not care if its ‘/’ at the end or not, I think it will not hard to modify it. As you can see this script loops through menu items and unsets the array element which has the url as it is our home url.

Nothing fancy in the script itself, but for a some the biggest problem could be to find what to modify, how to get that menu array, what filter to use. It took time for me at least.

Leave a Reply