one term two taxonomy's?

时间:2011-10-19 作者:Daithí

任何人都可以解决这个问题。。。

我正在构建一个应用程序,其中我有两个继承人自定义税种作为自定义类别。所以我有cat1和cat2分类法。

我想做的是将一个术语及其子项从cat1复制到cat2。可能类似于:

set_term_taxonomy($term_id, array("cat1, "cat2"));
同一术语及其子术语可能存在于多个分类法中。之所以有两种分类法,是因为它们是两种不同的车间库存。

1 个回复
SO网友:Daithí

必须编写一个方法来完成。以下是方法。。。希望它能帮助其他人;)

语法:

copy_terms($term->term_id, "taxonomy1", "taxonomy2", 0); //will copy to root of destination



 /**
 * Copy a term and its descendants from one taxonomy to another.
 * Both taxonomies have to be hierarchical. Will copy across 
 * posts as well.
 *
 * @param int $term the term id to copy
 * @param string $from the taxonomy of the original term
 * @param string $to the destination for the taxonomy
 * @param int $parent the parent term_id to add the taxonomy to
 * @return type true|WP_Error
 */
function copy_terms($term, $from, $to, $parent=0) {

    //check for child terms      
    $term = get_term($term, $from);
    $child_terms = get_terms($from, array(
        \'hide_empty\' => false,
        \'parent\' => $term->term_id
            ));

    //check for child products
    $child_prods = new WP_Query(array(
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => $from,
                    \'terms\' => $term->term_id
                )
            )
        ));

    //work out new slug
    if($parent==0) $slug = $to."-".sanitize_title($term->name);
    else{
        $parent_term = get_term($parent, $to);
        $slug = $parent_term->slug."-".sanitize_title($term->name);
    }

    //add term to new taxonomy
    $parent = wp_insert_term($term->name, $to, array(
        \'description\' => $term->description,
        \'parent\' => $parent,
        \'slug\' => $slug
            ));

    if(is_wp_error($parent)) return $parent;
    $parent = get_term($parent[\'term_id\'], $to);        
    if(is_wp_error($parent)) return $parent;

    //loop through folders first
    foreach($child_terms as $child){
        copy_terms($child->term_id, $from, $to, $parent->term_id);
    }

    //loop through products
    foreach($child_prods->posts as $child){
        wp_set_post_terms($child->ID, $parent->term_id, $to, true);
    }
    return true;

结束

相关推荐

List custom taxonomy terms

我正在尝试显示自定义分类法的所有术语(characters), 分类法与自定义帖子类型相关(books).我想做的是显示characters, 比如说我有两个books, 我加了三个characters 致各book, 我想显示所有六个字符。我只想得到名称,而不是链接,或者列表形式,如果我可以得到一组对象或一个更可取的数组。非常感谢。