Check what “type” a menu item is

Fortunately, there is an easy fix for this.

The $item in the Walker delivers Classes, and if the menu item is an archive, you get one of those:

  • menu-item-type-taxonomy (general)
  • menu-item-object-product_cat (product_cat == taxonomyname)

as well as

  • $item->type == ‘taxonomy’
  • $item->object == ‘your_taxonomy’

If you want this logic to work for various Archives (not just the product_cat) you need to change the taxonomy as well – again, $item conveniently delivers your $taxonomy as $item->object.

Now you only need to change your if and the taxonomy in your Walker

if( $item->type == 'taxonomy' ) {

    $non_empty_categories = get_categories( array( 'taxonomy' => $item->object ) );

    // ... The rest of your logic works fine

}

Should work just fine 🙂