2013-10-03 24 views
1

我可能會混淆術語,因此我希望您能遵循。wordpress在添加時獲得術語標題

我有一個自定義後類型的「套餐」 我已創建名作所謂的能力「添加封裝類型」

當我添加類別我想用的標題插入新的頁面只有在「添加軟件包類型」時才能使用新類別。

我似乎無法用下面的代碼獲取類別標題,在$ taxonomy裏面我得到了'Add Package Type',這很好。但我希望將網頁與我剛剛輸入的標題名稱相同。

感謝

function custom_create_packages($term_id, $tt_id, $taxonomy){ 

    if($taxonomy == 'package type'){ 
     $new_page_title = $cat_title; 
     $new_page_content = 'This is the page content'; 
     $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template. 
     //don't change the code bellow, unless you know what you're doing 
     $page_check = get_page_by_title($new_page_title); 
     $new_page = array(
      'post_type' => 'page', 
      'post_title' => $new_page_title, 
      'post_content' => $new_page_content, 
      'post_status' => 'publish', 
      'post_author' => 1, 
     ); 
     if(!isset($page_check->ID)){ 
      $new_page_id = wp_insert_post($new_page); 
      if(!empty($new_page_template)){ 
       update_post_meta($new_page_id, '_wp_page_template', $new_page_template); 
      } 
     } 
    } 
} 

add_action('create_term', 'custom_create_packages', 10, 3); 

回答

1

如果我理解正確的話,你就必須檢索基礎上,$term_id參數術語的詳細信息。喜歡的東西:

function custom_create_packages($term_id, $tt_id, $taxonomy){ 

    if($taxonomy == 'package_type'){ 
     $term = get_term($term_id, $taxonomy); 
     $new_page_title = $term->name; 

我不知道在你的代碼(「包類型」)的分類蛞蝓是否只是一個例子,但它不應該包含空格(見the Codex)。

可能值得將您的操作從create_term更改爲create_{taxonomy}(例如create_package_type),以免檢查分類標準。請注意,create_{taxonomy}只有2個參數($term_id$tt_id),因爲分類是已知的。

我不確定,但在created_{taxonomy}鉤子中調用函數可能會更安全(請注意'd'),該函數在clean_term_cache函數被調用後調用(儘管由於它是一個新條目,可能將不會有所作爲)。

相關問題