选择子术语时自动分配父术语

时间:2013-05-25 作者:Sisir

我有很深的层次分类法,我希望在选择子术语时分配所有父术语。我需要一个在线列表/分类网站的类别结构。

CPT name: product

Taxonomy Name: product_cat

4 个回复
最合适的回答,由SO网友:Sisir 整理而成

钩住save_post 行动

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\');
        }
    }
}
而循环确保我们向上走,直到达到顶级条款。

SO网友:Traveler

代码有一些上面提供的bug。使用以下代码也可以在“快速编辑”模式中工作:How to hook into the quick edit action? (提供人:Pieter Goosen)

add_action(\'save_post\', \'assign_parent_terms\');

function assign_parent_terms($post_id){
global $post;

if(isset($post) && $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_object_terms($post_id, array($term->parent), \'product_cat\', true);
    $term = get_term($term->parent, \'product_cat\');
     }
  }
}

SO网友:sMyles

上述选项应该可以工作,无论何时设置术语,此功能都可以用于任何对象,并使用set_object_terms 允许您让操作句柄向上遍历层次结构

add_action( \'set_object_terms\', \'auto_set_parent_terms\', 9999, 6 );

/**
 * Automatically set/assign parent taxonomy terms to posts
 * 
 * This function will automatically set parent taxonomy terms whenever terms are set on a post,
 * with the option to configure specific post types, and/or taxonomies.
 *
 *
 * @param int    $object_id  Object ID.
 * @param array  $terms      An array of object terms.
 * @param array  $tt_ids     An array of term taxonomy IDs.
 * @param string $taxonomy   Taxonomy slug.
 * @param bool   $append     Whether to append new terms to the old terms.
 * @param array  $old_tt_ids Old array of term taxonomy IDs.
 */
function auto_set_parent_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {

    /**
     * We only want to move forward if there are taxonomies to set
     */
    if( empty( $tt_ids ) ) return FALSE;

    /**
     * Set specific post types to only set parents on.  Set $post_types = FALSE to set parents for ALL post types.
     */
    $post_types = array( \'product\' );
    if( $post_types !== FALSE && ! in_array( get_post_type( $object_id ), $post_types ) ) return FALSE;

    /**
     * Set specific post types to only set parents on.  Set $post_types = FALSE to set parents for ALL post types.
     */
    $tax_types = array( \'product_cat\' );
    if( $tax_types !== FALSE && ! in_array( get_post_type( $object_id ), $post_types ) ) return FALSE;

    foreach( $tt_ids as $tt_id ) {

        $parent = wp_get_term_taxonomy_parent_id( $tt_id, $taxonomy );

        if( $parent ) {
            wp_set_post_terms( $object_id, array($parent), $taxonomy, TRUE );
        }

    }

}
如果我以后再添加,此要点将有任何更新或补丁:https://gist.github.com/tripflex/65dbffc4342cf7077e49d641462b46ad

SO网友:Marcio Dias

改进了!Sisir厚度

现在,您可以定义多个帖子类型和术语。

您可以在($arrayPostTypeAllowed)数组中定义允许的Post类型,并在($arrayTermsAllowed)数组中定义允许的术语。

add_action(\'save_post\', \'assign_parent_terms\', 10, 2);

function assign_parent_terms($post_id, $post){
    $arrayPostTypeAllowed = array(\'product\');
    $arrayTermsAllowed = array(\'product_cat\');

    //Check post type
    if(!in_array($post->post_type, $arrayPostTypeAllowed)){
        return $post_id;
    }else{

        // get all assigned terms   
        foreach($arrayTermsAllowed AS $t_name){
            $terms = wp_get_post_terms($post_id, $t_name );

            foreach($terms as $term){

                while($term->parent != 0 && !has_term( $term->parent, $t_name, $post )){

                    // move upward until we get to 0 level terms
                    wp_set_post_terms($post_id, array($term->parent), $t_name, true);
                    $term = get_term($term->parent, $t_name);
                }
            }
        }
    }
}

结束

相关推荐

Custom Post taxonomy template

朋友们,我需要帮助。我正在为朋友扩展Mayashop主题&;我遇到了一个问题。创建了一个名为watchdog的自定义帖子类型,为其创建了一个名为watchdog brand的分类法。。。现在是唯一的看门狗。php模板工作得很好,但我似乎找不到适合这个术语本身的模板,其中列出了它下面的所有看门狗项。已尝试存档监视程序。php,存档看门狗品牌。php但运气不好。。。有什么想法吗?我还发现它甚至没有回退到归档。php。。直接进入索引。php文件&;以下是custom\\u post的代码(&U);分