How to change the menu on a custom post type with the Menu Swapper plugin

Here’s how you can use the Menu Swapper for post types other than just Posts and Pages.

Written by

Chris Mavricos

Published on

May 21, 2013
BlogWordPress Tutorials

For those who are unfamiliar, the Menu Swapper plugin allows you to switch the menu you’re using on any individual Post or Page on your site. But what happens if you need to swap the menu on a custom post type?

Out of the box, the Menu Swapper will add a meta box to Posts and Pages (two standard post types) which allow you to choose which menu to display on the single post page. If you want to have the same on a custom post type, like a Portfolio Item for example, you’ll need to add that post type to the list of post types that the Menu Swapper will target.

The Menu Swapper makes this easy with the mswp_post_types filter. You’ll just need to add a few lines of code to your child theme’s functions.php. The main thing you’ll need is the custom post type ID – the easiest way to determine that if you’re not sure is to click on the “All {Post Type}” panel in your WordPress Admin Panel. The URL will contain the string ?post_type=portfolio; in this case the custom post type ID would be portfolio.

For the portfolio example, here’s what the PHP that you’d add to functions.php would look like:

function my_add_mswp_post_type( /* array */ $post_types ){

	$post_types[] = 'portfolio'; // 'portfolio' is the custom post type ID
	//$post_types[] = 'download'; // this is how you would add a second custom post type

	return $post_types;
}
add_filter( 'mswp_post_types', 'my_add_mswp_post_type' );

Add that code to your functions.php and you’ll get the Menu Swapper meta box on your custom post type, allowing you to change out the menu on that post type’s single page.

Enjoy!

Chris Mavricos

Hi, I'm Chris. I'm a web developer and founder of SevenSpark located in Boulder, CO. I've been developing websites since 2004, and have extensive experience in WordPress theme, plugin, and custom development over the last 15+ years.