WooCommerce: Add WP Menu to WC Pages via PHP

It’s pretty easy to add another custom menu to your WooCommerce pages via PHP. For example, you might want to add a list of Product Category links, and manage this through the default “Appearance > Menu” in the WordPress dashboard.

WP Dashboard > Appearance > Menu: create a new menu

Create a new menu called “Categories” and add the product categories you wish to it.

Create a new menu in WordPress

PHP Snippet: Add WP menu to WooCommerce pages

/**
 * @snippet       Add WP menu to WooCommerce pages
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=22012
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.5.2
 */

add_action( 'woocommerce_before_main_content', 'bbloomer_echo_categories_menu' );

function bbloomer_echo_categories_menu() {
wp_nav_menu( array( 'menu' => 'categories', 'container_id' => 'nav_menu-2' ) );
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

8 thoughts on “WooCommerce: Add WP Menu to WC Pages via PHP

  1. Hi, thanks for your help. I tried but I received this answer from wordpress

    Uncaught Error: Call to undefined function avia_get_header_scroll_offset() in wp-content/themes/enfold/css/dynamic-css.php:1005
    Stack trace:
    #0 wp-content/themes/enfold/includes/admin/register-dynamic-styles.php(159): require()
    #1 wp-content/themes/enfold/functions.php(696): avia_prepare_dynamic_styles()
    #2 wp-includes/class-wp-hook.php(287): avia_custom_styles('')
    #3 wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
    #4 wp-includes/plugin.php(478): WP_Hook->do_action(Array)
    #5 wp-settings.php(540): do_action('init')
    #6 wp-config.php(89): require_once('/web/htdocs/www...')
    #7 wp-load.php(37): require_once('/web/htdocs/www...')
    #8 wp-admin/admin.php(34): r
    

    I guess your code is causing a conflict with my theme? I’m using Enfold Theme

    1. I guess so

  2. THANK YOU for this! But Can I add a class to this? I want it to match my header menu with this same class:

    Where would I insert the code to make it pull this one instead?

    1. Hey Kimberly thanks for your comment! If you take a look at https://developer.wordpress.org/reference/functions/wp_nav_menu/, you can see you can also declare a parameter called “menu_class”. That should help ๐Ÿ™‚

  3. Hi Rodolfo,
    Thanks for the code!
    Is there a way to tweak this further, if we need a separate custom menu for each product?

    1. Hey Shruti, thanks for your comment! Yes, of course, you could take a look at my conditional logic guide to see how to target a specific product ID: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

  4. Thank you for the code ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.