Php 我还希望在父类别中显示子类别中的产品
这是一个Woocommerce网站商店,但当我在父类别中时,它不显示子类别的产品。一个解决方案是遍历所有产品,并将它们添加到父类别中,但当我有5000个产品时,这需要大量的工作Php 我还希望在父类别中显示子类别中的产品,php,wordpress,woocommerce,webshop,product-catalog,Php,Wordpress,Woocommerce,Webshop,Product Catalog,这是一个Woocommerce网站商店,但当我在父类别中时,它不显示子类别的产品。一个解决方案是遍历所有产品,并将它们添加到父类别中,但当我有5000个产品时,这需要大量的工作 有没有外汇。将所有产品添加到现有父类别的Sql查询 然后将method/function添加到function.php文件中,这样当我保存新产品时,它会自动添加到我选择的类别中的所有父类别中 我发现这个例子: add_action('save_post', 'assign_parent_terms', 10, 2);
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');
}
}
}
Mohammad Arshi从这个stackoverflow中得到了一个方法/函数的答案(),它可能是问题2的答案