2014-04-29 71 views
0

我想知道如何將現有頁面的列表添加到自定義分類中作爲條款。自定義分類和頁面列表

我的目標是能夠添加對上傳附件的頁面的引用。

我能夠創建分類,它顯示出來,它只是沒有填充。

另外 - 如果還有更好的方法可以去解決這個問題,

如: 我的頁面「信息」,「聯繫人」

我想這些項目顯示爲條件。

+0

我很困惑,你想添加這些網頁AS類別(分類術語)還是你想分配現有的網頁到你用你的定製分類法創建的某些類別? –

+0

前者。我希望能夠選擇附件「屬於」的頁面。 – gin93r

+0

那麼你可以勾選[保存帖子](http://codex.wordpress.org/Plugin_API/Action_Reference/save_post),當一個帖子創建後,你可以[插入一個詞](https://codex.wordpress.org/Function_Reference/wp_insert_term)與頁面標題匹配。這並不一定意味着兩者相連。我可以問你爲什麼想這樣做?目的是什麼?長遠來看,你希望它的功能如何 - 也許有更好的方法去實現它。 –

回答

0

我解決了這個問題,通過獲取頁​​面並使用wp_insert_term來插入它們。注意:我只需要一個特定的頁面,所以這比以上更具體。

// create a new taxonomy 
function add_product_pages_to_attachments() { 
    register_taxonomy(
     'productpages', 
     'attachment', 
     array(
      'hierarchical'=>true, 
      'label' => __('Assign to Product Page'), 
      'rewrite' => array('slug' => 'productpages'), 
      'show_in_nav_menus'=>false, 

     ) 
    ); 
} 
add_action('init', 'add_product_pages_to_attachments'); 

//populate the list 
function populate_product_list() 
{ 
    //get my specific page id which will be used in the 'parent' argument 
    $id = get_page_by_path("products")->ID; 
    $args = array(
     'post_type'=>'page', 
     'parent'=>$id 
    ); 
    //get all the pages matching the arguments above 
    $pages = get_pages($args); 
    //loop through the pages and add them to the taxonomy list 
    foreach ($pages as $page) { 
     wp_insert_term($page->post_title,'productpages'); 
    } 
    delete_option("productpages"); 
} 
add_action('init','populate_product_list');