从自定义字段添加父类别和子类别

时间:2014-08-11 00:10:11

标签: php wordpress post

这是我的功能代码,此功能的目的是通过自定义字段插入父类别和类别子代

function add_category_automatically() {
    global $wpdb;
    global $post;
    wp_set_object_terms($post_ID,'', 'category');
    $parent_category_name = get_post_meta( $post->ID, 'parent_custom_feild', true );
    wp_set_object_terms($post_ID,'$parent_category_name', 'category', true); //adding the parent category
    $parent_category_id=get_cat_ID("".$parent_category_name."");  // seeking the parent category id
    $child_category_name= get_post_meta( $post->ID, 'child_custom_feild', true );

    foreach((get_term_children( ''.$parent_category_id.'','category')) as $categoryy) {

            if (( get_cat_name ($categoryy)) == $child_category_name) 

            { $second_cat_id =get_cat_ID($child_category_name); }

                                         } // this will check if the child category exist and will pick the child category id

    $check=term_exists( "".$child_category_name."", 'category', $parent_category_id ) ; // check again if the child category exist 
    if ($check !== 0 && $check !== null) {
    wp_set_object_terms( $post_ID, $second_cat_id, 'category', true ); //adding custom feild to
    }
    else 
    $arg = array('description' => "Modifier", 'parent' =>$parent_category_id ); 
    $new_cat_id = wp_insert_term("".$nom_saison."", 'category', $arg); // creating a new  child category
    wp_set_object_terms( $post_ID, $new_cat_id, 'category', true ); // inserting post in the new child category id 
                    }

    add_action( 'wp_insert_post', 'add_category_automatically', 10, 1 );

此功能只插入父类请帮忙!!!

0 个答案:

没有答案