Remove Whitespace Between List Items Generated By wp_nav_menu()
Earlier today I was using wp_nav_menu() function within WordPress to output a navigation menu on a site. I was getting an issue however whereby the whitespace between the list items (<li>) was causing spaces between the menu items on the frontend. My list items had the ‘display’ CSS property set as ‘inline-block’ so I was getting visible gaps between them.
To conquer this I came up with the following snippet of code to strip out the whitespace:
echo preg_replace( '/li>\s+
Sticking with the normal use of wp_nav_menu(), instead of outputting it straight to the screen I set the ‘echo’ argument to FALSE (or zero) and run the output through preg_replace().