I am using Wordpress with Woocommerce, I am on the detail page of the product content-product-single.php.
I created several "sub" and I have to print ONLY subcategories associated with that product.
I am using this code:
$categories = array (
'type' => 'product',
'child_of' => 1,
'parent' => '8',
'orderby' => 'term_group',
'hide_empty' => true,
'hierarchical' = > 1,
'exclude' => '',
'property' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false);
$cats = get_categories ($categories);
foreach ($cats as $cat) {
echo '<a <br/> href="'. get_term_link($cat-> slug,' product_cat ').' "> '. $cat-> name.'</a>';
}
Where 'parent' => '8' is the 'id of my categories FATHER
But this code print ALL subcategories that are not empty, I am interested in only those instead of THIS PRODUCT.
Thanks and good luck
I am using Wordpress with Woocommerce, I am on the detail page of the product content-product-single.php.
I created several "sub" and I have to print ONLY subcategories associated with that product.
I am using this code:
$categories = array (
'type' => 'product',
'child_of' => 1,
'parent' => '8',
'orderby' => 'term_group',
'hide_empty' => true,
'hierarchical' = > 1,
'exclude' => '',
'property' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false);
$cats = get_categories ($categories);
foreach ($cats as $cat) {
echo '<a <br/> href="'. get_term_link($cat-> slug,' product_cat ').' "> '. $cat-> name.'</a>';
}
Where 'parent' => '8' is the 'id of my categories FATHER
But this code print ALL subcategories that are not empty, I am interested in only those instead of THIS PRODUCT.
Thanks and good luck
Share Improve this question asked May 25, 2015 at 7:07 user2663914user2663914 12 bronze badges 1- 1 Maybe my answer to Display product child categories from specific parent will help you. – Nicolai Grossherr Commented May 25, 2015 at 10:18
2 Answers
Reset to default 1Configure, product_id and parent_category_id according to your requirements.
/* This is product id */
$product_id = 1;
/* This is the id of the parent category */
$parent_category_id = 8;
$cats = get_categories ( array (
'type' => 'product',
'parent' => $parent_category_id,
'orderby' => 'term_group',
'hide_empty' => true,
'hierarchical' = > 1,
'exclude' => '',
'property' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false
));
$non_empty_sub_categories_ids = array_map(create_function('$cat', 'return $cat->term_id;'), $cats);
$product_cats = get_the_terms( $product_id, 'product_cat' );
foreach ($product_cats as $product_cat) {
if(in_array($product_cat->term_id, $non_empty_sub_categories_ids)) {
echo '<a <br/> href="'. get_term_link($product_cat->slug,' product_cat ').' "> '. $product_cat->name.'</a>';
}
}
<?php
$args = array(
'taxonomy' => 'product_cat',
'slug' => array('category_slug'),
'hide_empty' => true
);
$product_cat = get_terms( $args );
foreach ($product_cat as $parent_product_cat){
echo '<ul>
<li><a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
<ul> ';
$child_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => $parent_product_cat->term_id
);
$child_product_cats = get_terms( $child_args );
foreach ($child_product_cats as $child_product_cat) {
echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
}
echo ' </ul>
</li>
</ul>';
}
?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745004601a4605700.html
评论列表(0条)