2012-11-29 59 views
0

我知道如何創建自定義帖子類型。從查看Codex看來,我應該能夠創建一個自定義帖子類型,其行爲類似於頁面,特別是可以使用模板選擇器彈出窗口和類別/標籤選擇器來分配模板。使用模板選擇器(Page UI)創建自定義WordPress的發佈類型?

到目前爲止,我所得到的只是基本的編輯器,我可以得到一個精選的圖像選擇器。但我所尋找的基本上是一個頁面,我可以將其視爲自定義帖子類型。

編輯:我認爲這將是顯而易見的,但我是用的functions.php做:

register_post_type(hh_town, 
array(
    'labels' => array(
     'name' => __('Towns'), 
     'singular_name' => __('Town'), 
     'add_new' => _x('Add Town', 'towns'), 
       'add_new_item' => __('Add Town'), 
       'edit' => _x('Edit Towns', 'Towns'), 
       'edit_item' => __('Edit Town'), 
       'new_item' => __('New Town'), 
       'view' => _x('View Town', 'towns'), 
       'view_item' => __('View Town') 
    ), 
    'public' => true, 
    'has_archive' => true, 
    'hierarchical' => true, 
    'show_ui' => true, 
    'supports' => array('title','editor','page-attributes','thumbnail', 'custom-fields'), 
    'capability_type' => 'page', 
    'taxonomies' => array('post_tag','category') 
) 
); 
+0

好了,卻怎麼也你這樣做?插件或自定義代碼?請提供鏈接,屏幕截圖和/或示例代碼。 – brasofilo

+0

我編輯了我的問題以包含我正在使用的代碼。我很抱歉。 – Steve

回答

0

就在這個代碼添加到您的functions.php

add_action('add_meta_boxes', 'add_custom_page_attributes_meta_box'); 
function add_custom_page_attributes_meta_box(){ 
global $post; 
    if ('page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes')) { 
     add_meta_box('custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core'); 
    } 
} 

function custom_page_attributes_meta_box($post) { 
    $template = get_post_meta($post->ID, '_wp_page_template', 1); ?> 
    <select name="page_template" id="page_template"> 
     <?php $default_title = apply_filters('default_page_template_title', __('Default Template'), 'meta-box'); ?> 
     <option value="default"><?php echo esc_html($default_title); ?></option> 
     <?php page_template_dropdown($template); ?> 
    </select><?php 
} 

add_action('save_post', 'save_custom_page_attributes_meta_box'); 
function save_custom_page_attributes_meta_box($post_id) { 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 
    if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) return; 
    if (! current_user_can('edit_post', $post_id)) return; 
    if (! empty($_POST['page_template']) && get_post_type($post_id) != 'page') { 
     update_post_meta($post_id, '_wp_page_template', $_POST['page_template']); 
    } 
} 
相關問題