Search

Using custom attributes in menus and taxonomy archives

Attributes (which can be used for the layered nav) are a custom taxonomy, meaning you can display them in menus, or display products by attributes. This requires a little work on your part, but is fairly simple.

Register the taxonomy for menus ↑ Back to Top

When registering taxonomies for your custom attributes, WooCommerce calls the following hook:

$show_in_nav_menus = apply_filters('woocommerce_attribute_show_in_nav_menus', false, $name);

So, for example, if your attribute slug was ‘size’ you would do the following to register it for menus:

add_filter('woocommerce_attribute_show_in_nav_menus', 'wc_reg_for_menus', 1, 2);

function wc_reg_for_menus( $register, $name = '' ) {
     if ( $name == 'pa_size' ) $register = true;
     return $register;
}
Custom attribute slugs are prefixed with ‘pa_’, so an attribute called ‘size’ would be ‘pa_size’

Great. So now you can use your attribute in Appearance > Menus. You will notice however that when you click on a link to your taxonomy term it has default blog styling…

Create a template ↑ Back to Top

You will need to theme your attribute to make it display products how you want. To do this:

  1. Copy woocommerce/templates/taxonomy-product_cat.php into your theme folder
  2. Rename the template to reflect your attribute – in our example we’d useĀ taxonomy-pa_size.php

Thats all there is to it. You will now see this template when viewing taxonomy terms for your custom attribute.

Back to the top