在 Woocommerce 中显示父级下的产品子类别
Show Sub categories of products under parent in Woocommerce
我正在尝试找到一种方法来在管理面板下轻松管理我的产品类别。
目前所有类别都列在同一位置。我需要的是当我点击一个父类别时我可以看到所有的子类别并且能够添加新的子类别。
使用下面的代码
add_action('save_post', 'assign_parent_terms', 10, 2);
function assign_parent_terms($post_id, $post){
if($post->post_type != 'product')
return $post_id;
// get all assigned terms
$terms = wp_get_post_terms($post_id, 'product_cat' );
foreach($terms as $term){
while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
// move upward until we get to 0 level terms
wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
$term = get_term($term->parent, 'product_cat');
}
}
}
以上代码也会将子类别产品分配到父类别中
现在使用免费插件:https://wordpress.org/plugins/product-category-tree/。这些天试用和测试它并按预期工作。
P.S。老问题,但我一直在寻找相同的答案,但没有找到 - 希望它也能帮助其他人
我正在尝试找到一种方法来在管理面板下轻松管理我的产品类别。
目前所有类别都列在同一位置。我需要的是当我点击一个父类别时我可以看到所有的子类别并且能够添加新的子类别。
使用下面的代码
add_action('save_post', 'assign_parent_terms', 10, 2);
function assign_parent_terms($post_id, $post){
if($post->post_type != 'product')
return $post_id;
// get all assigned terms
$terms = wp_get_post_terms($post_id, 'product_cat' );
foreach($terms as $term){
while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
// move upward until we get to 0 level terms
wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
$term = get_term($term->parent, 'product_cat');
}
}
}
以上代码也会将子类别产品分配到父类别中
现在使用免费插件:https://wordpress.org/plugins/product-category-tree/。这些天试用和测试它并按预期工作。
P.S。老问题,但我一直在寻找相同的答案,但没有找到 - 希望它也能帮助其他人