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!
Hi,
Do you know if your menu plug in has the function on the two links below:
It is the tab in the article see below where the arrow is and if you can just check the site to see what it does.
http://www.tydonsafaris.com/3-day/tydon-camp.html
or this one
http://www.acacia-africa.com/acacia_small_groups/masai_mara_safari_3_days_2013.php
I don’t want to buy your product if I am unable to add a separate menu in a actual page or post
Thanks
Tania
Hi Tania,
I don’t understand what you are asking. The plugin relevant to this article – the Menu Swapper – is freely available in the WordPress Plugin Repository. I have multiple other menu plugins, so if you’re not referring to this one, I don’t know which you are asking about.
The Menu Swapper allows you to use different menus on different Pages and Posts, by replacing an existing menu.
Chris
Hi Chris,
I have just copied this code into the child’s theme functions.php – and I always experience a Server 500 error afterwards.
What could be the reason for that?
Sounds like you have a PHP error. This code won’t produce an error itself, so that likely means you have added it incorrectly in functions.php and created a syntax error. Make sure you add it to the end of the file, but inside the PHP tags.
You can enable debugging on your site to see the exact PHP error.
Have a great weekend,
Chris
Hi I did what you say, but it seems that something went wrong, cause now I have all my menues loading in all the pages. Any clues on how to fix this?
Hello, So i manage to make it work, but now I’m stuck cause I need my woocommerce product category pages to swapp menus too, any idea of how to make this work?
Hi Roberto,
The Menu Swapper only works on posts, not on taxonomy / archive pages, since those don’t have edit screens in WordPress, sorry. For those you’d need to edit the templates.
Chris