This may seem simple but I want to create a shortcode called [woo_prod_categories] that will simply display a list of the categories of a specific product on the single product page (each linked to their category page).

I did this for the SKU:

function display_woo_cat() {

global $product;
return $product->get_sku();

add_shortcode( 'woo_prod_category', 'display_woo_cat' );

I can tried the shortcodes I found in the WooCommerce documentation but they just show all the categories. I know there are ways to do this via standard single product pages turning on options in the WooCommerce settings. I am trying to make a custom single product page. I am using elementor and I know there are elements that can do this. I just want a list of categories that are linked back to the category page that are produced by a shortcode.

I want to put this shortcode in a table so it matches the meta data like attributes, dimensions, etc.

Does anyone have any idea how I can do this quick and easy?

1 Answer 1


You can try this:

function woo_prod_categories() {
    if ( is_product() ) {
        global $post;
        $product_cats = get_the_terms( $post->ID, 'product_cat' );
        if ( ! empty( $product_cats ) && ! is_wp_error( $product_cats ) ) {
            $cat_links = array();

            foreach ( $product_cats as $product_cat ) {
                $cat_links[] = '<a href="' . get_term_link( $product_cat->term_id, 'product_cat' ) . 
                '">' . $product_cat->name . '</a>';

            return implode( ', ', $cat_links );

    return '';

add_shortcode( 'woo_prod_categories', 'woo_prod_categories' );

The function checks if the current page is a product page and then retrieves the product categories (using get_the_terms() function). It then generates a linked list of these categories which is then returned by the shortcode. The categories are separated by commas.

  • This is great! Thank you, thank you! Is there anyway for me to set them in the order I have set in the categories section of WooCommerce or does it have to be alphabetical? May 23, 2023 at 17:08
  • Exactly where should this code be added? It's for a regular page, not a post.
    – andreszs
    Jul 18, 2023 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.