admin管理员组文章数量:1323715
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 )));
}
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 )));
}
Share
Improve this question
asked Sep 11, 2020 at 0:04
KorlaKorla
11 bronze badge
1 Answer
Reset to default 0Check 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 )));
}
本文标签: phpGet parent category name when only child category is applied to a product
版权声明:本文标题:php - Get parent category name when only child category is applied to a product 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742125679a2421928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论