简体   繁体   English

WordPress按名称和父类别设置产品类别

[英]Wordpress set product category by name and parent category

I need to set product category by name 我需要按名称设置产品类别

but i have two different sub-category with same name but different parent cat 但我有两个不同的子类别,它们的名称相同,但母猫却不同

I use : 我用 :

wp_set_object_terms($post_id, $my_categories, 'product_cat');

but in this way wordpress set the sub-cat with low id not the right sub-cat for relative parent cat 但是以这种方式wordpress设置了具有低id的子猫,而不是相对父猫的正确子猫

eg. 例如。 : T-shirt (parent category is Woman) and T-shirt (parent category is Man) :T恤(父类别为“女人”)和T恤(父类别为“男人”)

How can I set the right sub-category to the product? 如何为产品设置正确的子类别?

You can set category list from your parent category ID. 您可以从父类别ID设置类别列表。

Please check below code: 请检查以下代码:

$args = array('parent' => 17); // Here pass your parent category ID(Man or Female)
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
}

In this way, You will get all the category name with other details from parent one. 这样,您将从父目录中获得所有类别名称以及其他详细信息。

This way you can display T-shirt category from different male and female category. 这样,您可以显示来自不同男性和女性类别的T恤类别。

Hope this will helpful for you. 希望这对您有所帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM