# "Back to Category" Link Above Products

> Adds a "Back to Shoes" style link above each product page that returns the shopper to the product's main category.

- Type: PHP
- Runs: Wc before single product
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, product-page, navigation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-back-to-category-link/

## Code

```php
$product_id = get_the_ID();
$term_id    = 0;

foreach ( array( '_yoast_wpseo_primary_product_cat', 'rank_math_primary_product_cat' ) as $meta_key ) {
    $term_id = $term_id ? $term_id : (int) get_post_meta( $product_id, $meta_key, true );
}

$term = $term_id ? get_term( $term_id, 'product_cat' ) : null;

if ( ! $term || is_wp_error( $term ) ) {
    $terms = get_the_terms( $product_id, 'product_cat' );
    $term  = $terms && ! is_wp_error( $terms ) ? $terms[0] : null;
}

if ( $term ) {
    printf( '<p class="wpide-back-to-cat" style="margin:0 0 12px;"><a href="%1$s">&larr; Back to %2$s</a></p>', esc_url( get_term_link( $term ) ), esc_html( $term->name ) );
}
```

## Notes

Uses the product's first category, or the primary category set by Yoast SEO or Rank Math when available.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find ""Back to Category" Link Above Products" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
