2012-08-29 41 views
-1

我創建一個插件類進口商在WordPress我有三個陣列WordPress的插件父子關係

<?php 
$Id={'1','2,'3','4','5'}; 
$Title={'Electronic','Mobile','Iphone','Freezer','Lg'}; 
$Catid={'0','1','2','1','4'}; 
?> 

現在使用它們的孩子,我怎麼保持父子關係是那些$ CATID用$ ID匹配意味着移動是電子的孩子。 我需要將它們插入數據庫並在Wp-admin中顯示關係。 我卡在這裏任何幫助是可觀的?

回答

0

我在我的代碼中使用 以下約定

$Id as $myid 
$Title as $mytitle 
$Catid as $catid 

    for($i=0;$i<count($myid);$i++) 
    { 

     if($catid[$i]==0) 
     { 
      $my_cat = array('cat_name' => $mytitle[$i], 'category_description'  => 'A Cool Category', 'category_nicename' => $mytitle[$i],   'category_parent' => ''); 
      // Create the category 
      $my_cat_id = wp_insert_category($my_cat); 
     } 
     else { 
      for($j=0;$j<count($myid);$j++) 
      { 
       if($myid[$j]==$catid[$i]){ 
        $parent=trim($mytitle[$j]); 
        break; 
       } 

      } 

      $query="SELECT `term_id` 
FROM `wp_terms` 
WHERE `name` = '$parent'"; 

      $res=$wpdb->get_results($query); 

      foreach($res as $result) 
      { 
       $parent_id=$result->term_id; 
      } 

      //var_dump($res);die; 
      $my_cat = array('cat_name' => $mytitle[$i], 'category_description'  => 'A Cool Category', 'category_nicename' => $mytitle[$i],   'category_parent' => $parent_id); 
      $my_cat_id = wp_insert_category($my_cat); 
     } 

    }