skip to Main Content

Is it possible to add a Woocommerce product category to the menu dynamically?
How can I do this? I am trying to add woocommerce product category to the menu dynamically. I am expecting a good solution to this problem. I am Unable to do this.

2

Answers


  1. There are a few ways to do this using the available WordPress hooks. One such hook you can use is the wp_nav_menu filter (see documentation). This hook allows you to directly modify the output of the nav menu HTML. Be careful, as this will affect all menus unless you set a condition based on the args from the 2nd argument in the filter.

    An example implementation

    add_filter('wp_nav_menu', 'example_func',10,2);
    
    function example_func(string $nav_menu, $args): string
    {
        // Get categories
        $categories = get_categories(['taxonomy' => 'product_cat']);
    
        //create new unordered list and append to end of navigation.
        $nav_menu .= '<ul>';
        foreach($categories as $cat) {
           $nav_menu .= '<li>' . $cat->name . '</li>'
       }
       $nav_menu .= '</ul>';
    
    return $nav_menu;
    

    If you are wanting to place the products in a specific place, you may be better off looking at creating a custom Nav Walker see docs or changing your filter to a more precise spot.

    Login or Signup to reply.
  2. May I have an idea of how do I implement it? I’m very beginner on WP and PHP, that’s the challenge! I’d like to know how may I implement those changes, but not directly to the "CORE" of WP or even the core of Woocommerce plug-in (if it’s that way I’m imagining that it works in general, by the way). In order to, when it had an update, my code wouldn’t be overrided (reset) by the new updated version of the plug-in/wordpress.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search