- Add this code in the functions.php file of you theme or child theme
function product_category_link_shortcode() {
if (!function_exists(‘wc_get_product’)) {
return ”; // WooCommerce not active
}
global $product;
if (!$product || !is_a($product, 'WC_Product')) {
return '';
}
$terms = get_the_terms($product->get_id(), 'product_cat');
if (!empty($terms) && !is_wp_error($terms)) {
$output = '<p class="product-category-link">Explore more in: ';
$links = [];
foreach ($terms as $term) {
$term_link = get_term_link($term);
$links[] = '<a href="' . esc_url($term_link) . '">' . esc_html($term->name) . '</a>';
}
$output .= implode(', ', $links);
$output .= '</p>';
return $output;
}
return '';
}
add_shortcode(‘product_category_link’, ‘product_category_link_shortcode’);
2. Add this shortcode to place where you want to show your dynamic category link
[product_category_link]
Category:
