How to get the slug of the parent category regardless of nesting?

I have a parent category Clothing and footwear
It has 2 subcategories
Men
-Clothing
Shoes
Women
-Clothing
Shoes

Get the category like this:
$current_category = get_queried_object();

$args_category = [
 'taxonomy' => $current_category->taxonomy,
 'parent' => $current_category->term_id,
 'hide_empty' => false,
];

$terms = get_terms( $args_category );

if ( $terms ) {
 foreach ( $terms as $term ) {
 if ( $term->parent > 0 ) {

}
}
}

How can I get the name (slug) of the parent category clothing and footwear, regardless of nesting ?
April 7th 20 at 10:51
1 answer
April 7th 20 at 10:53
Solution
$current_category->slug;
I need to get the slug of the parent category

Let's say the person is in the category of clothing or shoes, etc.

The fact that I need to constantly check on what category the person comes, I need to get the parent category - richie.Gerho commented on April 7th 20 at 10:56
$current_category->slug get slug of the current category

For example if the person is at the current category clothing and footwear you will be clothes and shoes

But if the category is Shoes, then it displays the slug of the category Shoes

And I need to get the parent category regardless of nesting - richie.Gerho commented on April 7th 20 at 10:59
if ($current_category->parent) {
 $term = get_term( $current_category->parent, $current_category->$taxonomy,);
 if ( ! is_wp_error($term) && $term) {
$term->slug;
}
}
- josefina83 commented on April 7th 20 at 11:02
@josefina83, very strange, I decided to look print_r($term->slug);

And here's what happened:
5e405073564ff997807844.png
5e404fe4399ba275623447.png

Ie again is the problem, clothes and shoes are everywhere, but what about get_ancestors() ? - richie.Gerho commented on April 7th 20 at 11:05
what's odd about it? Returns the parent term. All right - josefina83 commented on April 7th 20 at 11:08
@josefina83, on the second screenshot was not displayed parent category: clothing and shoes

I need the parent category is always displayed, regardless of nesting - richie.Gerho commented on April 7th 20 at 11:11
Then run in a loop until you get the top parent term when checking for 0 - josefina83 commented on April 7th 20 at 11:14
@josefina83, right ?

if ( $term->parent > 0 ) {
 $term_category = get_ancestors( $term->term_id, 'product_cat' );
}
- richie.Gerho commented on April 7th 20 at 11:17
it is possible and so.
So even faster.
Just look the documentation, there is a 3rd argument. - josefina83 commented on April 7th 20 at 11:20
@josefina83, so the problem is that get_ancestors only displays the id of the categories (including the parent), not slugs

How, then, to slug ? - richie.Gerho commented on April 7th 20 at 11:23
View code Above. There is get_term - josefina83 commented on April 7th 20 at 11:26
@josefina83, in General, and so I did

But I thought that your example would be better ) - richie.Gerho commented on April 7th 20 at 11:29
@josefina83, another wanted to know, right, what is needed in the loop checks to do ?

if ( $terms ) {
 foreach ( $terms as $term ) {
 if ( $term->parent > 0 ) {
 $term_category = get_ancestors( $term->term_id, 'product_cat' );
}
 if ( !$term_category['odezhda-i-obuv'] && !is_product_category( 'odezhda-muzhchinam') ) {

 } else {
}
}
}
- richie.Gerho commented on April 7th 20 at 11:32
@josefina83shorter get_ancestors in my case will not work, because I have woocommerce_page_title() inside the loop is duplicated constantly

One way to do it ?
$current_category = get_queried_object();

$args_category = [
 'taxonomy' => $current_category->taxonomy,
 'parent' => $current_category->term_id,
 'hide_empty' => false,
];

$categories = get_the_terms( $product->get_id, 'product_cat' );
$terms = get_terms( $args_category );

if ( $terms ) {
 if ( $categories->slug != 'odezhda-i-obuv' && !is_product_category( 'odezhda-muzhchinam' ) ) {
 foreach ( $terms as $term ) {

}
}
}
- richie.Gerho commented on April 7th 20 at 11:35
To be honest, I don't understand what You wrote here.

You should have 2 pieces of code for the archive page (starnicy category) and the item card.
the block for archive page You already have, ostanovi to add to the item card.
Apparently You try to collect all in a heap. Better not to do it.

Note that different types of pages get_queried_object() will give different types of data - josefina83 commented on April 7th 20 at 11:38
@josefina83,
Note that different types of pages get_queried_object() will give different types of data

that's all I need )

All the checks I do in the file taxonomy-product_cat.php

I just have the categories layout, for example in clothes and shoes one layout, and household goods - other

Only change the category layout of the goods does not change - richie.Gerho commented on April 7th 20 at 11:41

Find more questions by tags WordPressWooCommerce