0

I'm trying to get the parent category of a product when actually it's assigned to a child category of the parent, but not the parent itself. The function below works when I assign both the child and parent category for the product. But I want only to assign the product to the child category and in the same time to display the parent category too. Like that in this order: ChildCatName - ParentCatName Could you please point me in the right direction? Thank you!

    //Woo Cat product
    $woo_single_cats = get_the_terms( $post->ID, 'product_cat' );
    
    if ( $woo_single_cats && ! is_wp_error( $woo_single_cats ) ) {
        
        $woo_single_cat = array();
        
        foreach ( $woo_single_cats as $term ) {
            $woo_single_cat[$term->term_id] = $term->name;
        }

        $woo_single_cat = apply_filters('seopress_titles_product_cat', $woo_single_cat);

        $woo_single_cat_html = stripslashes_deep(wp_filter_nohtml_kses(join( " - ", $woo_single_cat )));
    }
0

Check if it works for you... i have modified the loop
/Woo Cat product $woo_single_cats = get_the_terms( $post->ID, 'product_cat' );

    if ( $woo_single_cats && ! is_wp_error( $woo_single_cats ) ) {
        
        $woo_single_cat = array();
        
        foreach ( $woo_single_cats as $term ) {
            $woo_single_cat[$term->term_id] = $term->name;
            $term_parent_id = $term->parent;
            $parent_term  = get_term($term_parent_id, 'product_cat');
              
        }

        $woo_single_cat = apply_filters('seopress_titles_product_cat', $woo_single_cat);

        $woo_single_cat_html = stripslashes_deep(wp_filter_nohtml_kses(join( " - ", $woo_single_cat )));
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.