Created
August 4, 2024 06:51
-
-
Save braddalton/985ca9ff78bd94020d819152ce7c5dec to your computer and use it in GitHub Desktop.
How To Limit Category Link on Product Page in WooCommerce ( On Non Block Themes )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40); | |
add_action('woocommerce_single_product_summary', 'custom_display_limited_product_categories', 40); | |
function custom_display_limited_product_categories() { | |
global $product; | |
$categories = wp_get_post_terms( $product->get_id(), 'product_cat', array('orderby' => 'name', 'order' => 'ASC', 'number' => 1)); // Limit to 3 categories | |
if ( $categories && !is_wp_error( $categories ) ) { | |
$cat_links = array(); | |
foreach ($categories as $category) { | |
$cat_links[] = '<a href="' . esc_url(get_term_link($category->term_id, 'product_cat')) . '">' . esc_html($category->name) . '</a>'; | |
} | |
echo '<span class="posted_in">' . implode(', ', $cat_links) . '</span>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment