2011-04-29 33 views
1

我有一個主要用作CMS的wordpress項目。感謝這個美妙的社區,我已經從對WordPress的一無所知到能夠創建自定義帖子類型(用於我的產品列表),使用分層分類法對它們進行分類,併爲此產品帖子類型創建自定義字段。WordPress CMS - 可能會使'動態'自定義帖子類型?

現在,我的產品非常簡單。他們有ProductNameProductTypeProductCategory,DocumentNameDocumentURL。後兩個是相關的,因爲DocumentURL是指向網絡上PDF的鏈接,而DocumentName是DocumentURL的標籤。我爲我的DocumentNameDocumentURL創建了自定義字段,並且可以爲每個Product自定義帖子添加1個。但是,我的Product可能有很多文檔URL和文檔名稱,或者它可能有1個,甚至是0個。有沒有辦法讓它變成動態的,因此無論我有多少?或者,我是否需要提供最大數量併爲產品自定義帖子創建許多自定義字段?

如果這只是直的PHP或ASP.NET我只是爲Document元素創建一個單獨的數據庫表,並將一個好的表單與一些以2或3個文檔字段開頭的jQuery組合在一起,如果他們需要更多的可以單擊+符號添加另一行文檔字段(如此http://deepliquid.com/projects/appendo/demos.php)。然後循環遍歷它們並將它們添加到數據庫中。這樣的事情可以用WordPress嗎?

我唯一的想法是爲文檔創建一個新的自定義文章類型並創建與他們的產品的關係,但是我無法圍繞如何工作。

任何意見將不勝感激!謝謝!

回答

4

我會假設你對PHP感到滿意,因爲你在這裏說了什麼。好消息是您的PHP知識將在這裏派上用場。是時候學習register_post_type函數的新功能了;即register_meta_box_cb參數。這使您可以定義一個函數來調用以在cpt添加和編輯頁面上添加元框。舉例來說,這裏是一個CPT(從手抄本直取)與所添加的參數:

add_action('init', 'codex_custom_init'); 
function codex_custom_init() 
{ 
    $labels = array(
    'name' => _x('Books', 'post type general name'), 
    'singular_name' => _x('Book', 'post type singular name'), 
    'add_new' => _x('Add New', 'book'), 
    'add_new_item' => __('Add New Book'), 
    'edit_item' => __('Edit Book'), 
    'new_item' => __('New Book'), 
    'view_item' => __('View Book'), 
    'search_items' => __('Search Books'), 
    'not_found' => __('No books found'), 
    'not_found_in_trash' => __('No books found in Trash'), 
    'parent_item_colon' => '', 
    'menu_name' => 'Books' 

); 
    $args = array(
    'labels' => $labels, 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true, 
    'rewrite' => true, 
    'capability_type' => 'post', 
    'has_archive' => true, 
    'hierarchical' => false, 
    'menu_position' => null, 
    'register_meta_box_cb' => 'my_meta_box_function', 
    'supports' => array('title','editor','author','thumbnail','excerpt','comments') 
); 
    register_post_type('book',$args); 
}} 

現在,你有元盒CB的功能定義,使用它像:

function my_beta_box_function(){ 
    add_meta_box(
     'myplugin_sectionid', 
     __('My Post Section Title', 'myplugin_textdomain'), 
     'myplugin_inner_custom_box', 
     'book' 
    ); 
} 

的add_meta_box爲您定義了一個元框,並提供了一個在metabox內創建內容的功能。在這種情況下,我們的代碼指的是功能myplugin_inner_custom_box。有關添加元框的更多信息,請參閱http://codex.wordpress.org/Function_Reference/add_meta_box。所以,我們現在需要通過定義功能來添加我們的內容:

function myplugin_inner_custom_box() 
{ 
    // Everything here would appear in the meta box 
} 

在這一點上,你可以利用你的PHP知識來創建可以在後處理提交的HTML表單。您可以添加您的+按鈕並使用您的PHP,就像您在任何其他PHP應用程序中一樣。我不會去做如何做到這一點,因爲我會假設你可以做到這一點。實際上,我已經創建了一些這樣的metabox,並且知道我發佈的函數之後,我可以依靠我的PHP知識。最後一塊是保存輸入。通過使用定義您的日常保存和save_post動作的功能,你可以用你的PHP知識來保存數據:

/* Do something with the data entered */ 
add_action('save_post', 'myplugin_save_postdata'); 

/* When the post is saved, saves our custom data */ 
function myplugin_save_postdata($post_id) { 
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 

    if (!wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__))) 
     return $post_id; 


    // Check permissions 
    if ('page' == $_POST['post_type']) 
    { 
    if (!current_user_can('edit_page', $post_id)) 
     return $post_id; 
    } 
    else 
    { 
    if (!current_user_can('edit_post', $post_id)) 
     return $post_id; 
    } 

    // OK, we're authenticated: we need to find and save the data 

    $mydata = $_POST['myplugin_new_field']; 

    update_post_meta('my_field', $mydata); 

    return $mydata; 
} 

最後,在保存功能的說明。如果您使用數組值(例如,docs[]),$ _POST ['docs']的值將是一個數組。幸運的是,WP的update_post_meta函數都設置爲處理數組輸入。它會序列化並輸入它。使用兼容的get_post_meta函數將反序列化數組以供數據輸出使用。

祝你好運!

+0

哇謝謝你!讓我一步一步閱讀。再次感謝! – drpcken 2011-04-29 16:18:17

+0

所以這些元框沒有連接到任何自定義字段嗎?我將不得不創建我自己的表爲metaboxes的值並將它們鏈接到帖子ID? – drpcken 2011-04-29 17:05:48

+0

哦,等等,我想我明白了,它是動態添加自定義元框!非常好!我會與此合作。謝謝! – drpcken 2011-04-29 18:18:12

相關問題