I’m posting this little tip for my future self as much as for anyone else.
The problem: It seems that in recent years WordPress has all-but-deprecated the
wp_page_menu() function in favor of the
wp_nav_menu() function, but the latter is primarily intended for Custom Menus, and there are some cases where Custom Menus aren’t what you want.
I’m working on a couple of sites right now that have a lot of pages, and I don’t want their editors to have to go over to Appearance > Menus every time they add or move a page. I’m relying on the CMS Tree Page View plugin on these sites to allow the clients to leverage the built in Parent / Order settings on pages to manage their primary navigation, rather than managing a separate Custom Menu.
Since I’m not using a Custom Menu, I don’t want to use
wp_nav_menu() at all; I pretty much have to use
wp_page_menu(). Which is fine. Except for the fact that the site designs do not call for having Home in the navigation.
No problem, right? After all, I can just add
'show_home' => false to the arguments passed to the function, and it will remove the home page.
But it doesn’t.
I’m not entirely sure what’s going on in the WordPress core here, but I suspect that
show_home only works if you’re using the old school default configuration of WordPress that calls for the home page being the main “posts” page. And who does that anymore? If you’ve created a custom home page, even if you’ve configured WordPress properly to display it as the main page, this function doesn’t care. (Do functions “care” about anything? But I digress…)
Remember also that there’s an
exclude argument, to which you can pass a comma-delimited string of post IDs to exclude. That works, but… ugh. You mean I really have to hardcode the post ID of the home page right into my header.php file?
Of course not. Use this:
get_option('page_on_front') and it will automatically find the home page. This is great if you want to be able to reuse a theme or custom function on multiple sites or just not commit the transgression of hardcoding something that really doesn’t need to be hardcoded.
Here’s the complete code sample. Note there’s no need to bother with
show_home at all.
'exclude' => get_option('page_on_front'),