Get only one product category term for a WooCommerce product

Bert

I have a mystery with the categories from WooCommerce. For different products I have multiple categories. For example the product Nike Air Red I connect this product with two categories Brands->Nike and Shoes->Red Brands and Shoes are main categories and Nike and Red are subcategories.

On the product page I have the following code

$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    echo $term->name;
}

The output is NikeRed

Is there a way to get one category? Nike or Red?

I tried also

get_ancestors(get_queried_object_id(), 'product_cat')

But this array is empty

LoicTheAztec

Try to use reset() to get the first term as follows:

$term_names = wp_get_post_terms( $post->ID, 'product_cat' array('fields' => 'names') );
echo reset($term_names);

This will display the first term name.

And using end() will display the last one:

$term_names = wp_get_post_terms( $post->ID, 'product_cat' array('fields' => 'names') );
echo end($term_names);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get a product from specific product attribute term name in WooCommerce

Get a list of siblings term ids from current product category in WooCommerce

How can I set tab (only) if the product is in a certain category WooCommerce

Get "primary" category image from WooCommerce product

Get the Product tags for the current product only in WooCommerce

Get the product category name and description in Woocommerce Single Product page

Get all Woocommerce products from current product category term Id in a WP_Query

Custom decimals in WooCommerce product prices for a product category

WooCommerce product price visibility: add price back only for a specific category

Get specific product attribute values for a specific product category in Woocommerce

Add the term slug as a class to the product category div tag in Woocommerce

Get Woocommerce product category image url

Allow only one product purchase for a specific product category in Woocommerce

Set a product category term in a product on Woocommerce

List main product subcategories of a product category in WooCommerce

Check a product category for a product in Woocommerce

Query by product category, product tags and price in Woocommerce

How to remove product thumbnail if only one image is added to product WooCommerce

Get the subcategories of the current product category in Woocommerce archives

If product category has children remove permalink from parent term in WooCommerce

Woocommerce: Product Category Widget

woocommerce get product category id

Woocommerce - get product in category with specific attribute

Woocommerce Add only 1 product based on category

Getting the 'thumbnail ID' from last product category term in WooCommerce

Check if a product belongs to a specific product category in Woocommerce

Get and display the product category featured image in Woocommerce

Is there a way to get all the category slug of a woocommerce product

Woocommerce - Displaying only the parent category under the product title