2014-09-01 132 views
0

我写了这样的插件演示puproses,但为什么在管理面板我没有看到这个额外的领域?我做错了什么?与metabox的自定义类型不显示附加字段

我做了自定义数据:汽车零部件,并在那里,我必须有两个附加字段:重量,品牌(分类),价格。

<?php 
/* 
Plugin Name: spare parts 
Plugin URI: http://wp.tutsplus.com/ 
Description: descr 
Version: 1.0 
Author: Mr.Who 
Author URI: http://wp.tutsplus.com/ 
License: GPLv2 
*/ 
add_action('init', 'spare_part_init'); 

function spare_part_init() 
    { 


    $args = array(
    'label' => __('Spare parts') , 
    'labels' => array(
     'edit_item' => __('Edit Part') , 
     'add_new_item' => __('Add New Part') , 
     'view_item' => __('View Part') , 
    ) , 
    'singular_label' => __('Spare part') , 
    'public' => true, 
    'show_ui' => true, 
    '_builtin' => false, 
    'capability_type' => 'post', 
    'hierarchical' => false, 
    'rewrite' => array(
     "slug" => "spare_parts" 
    ) , // формат ссылок 
    'supports' => array(
     'title', 
     'editor', 
     'thumbnail' 
    ) 
); 


    register_post_type('spare_parts', $args);  
    register_taxonomy('mtype', 'brand', array(
    'hierarchical' => true, 
    'label' => __('Brands') , 
    'singular_label' => __('Brand') , 
    'query_var' => 'mtype' 
)); 
    } 

add_action("admin_init", 'spare_parts_admin_init'); 

function spare_parts_admin_init() 
    { 
    add_meta_box("part-meta", "Description", part_options, 'part', 'normal', 'low'); 
    } 

function part_options() 
    { 
    global $post; 
    echo '<input type="hidden" name="part_noncename" id="part_noncename" value="' . wp_create_nonce('part') . '" />'; 
    $my_fields = array(
    'weight' => '', 
    'brand' => '', 
    'price' => '' 
); 
    foreach($my_fields as $key => $value) 
    { 
    $my_fields[$key] = get_post_meta($post->ID, 'part-' . $key, true); 
    } 

    echo '<strong>Weight</strong><br/><br/><input name="part-weight" size="60" value="' . $my_fields['weight'] . '" /><br/><br/>' . "\n"; 
    echo '<strong>Brand</strong><br/><br/><input name="part-brand" size="60" value="' . $my_fields['brand'] . '" /><br/><br/>' . "\n"; 
    echo '<strong>Price</strong><br/><br/><input name="part-price" size="60" value="' . $my_fields['price'] . '" /><br/><br/>' . "\n"; 
    } 

add_action('save_post', 'part_save', 1, 2); 

function part_save() 
    { 
    global $post; 
    $post_id = $post->ID; 
    if (!wp_verify_nonce($_POST['part_noncename'], 'part')) return $post_id; 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; 
    $post_flag = 'x'; 
    if ($post->post_type == "part") 
    { 
    $my_fields = array(
     'weight' => '', 
     'brand' => '', 
     'price' => '' 
    ); 
    $post_flag = 'part'; 
    } 

    if ($post_flag != 'x') 
    { 
    foreach($my_fields as $k => $v) 
     { 
     $key = 'part-' . $post_flag . '-' . $k; 
     $value = @$_POST[$key]; 
     if (empty($value)) 
     { 
     delete_post_meta($post_id, $key); 
     continue; 
     } 

     if (!is_array($value)) 
     { 
     if (!update_post_meta($post_id, $key, $value)) 
      { 
      add_post_meta($post_id, $key, $value); 
      } 
     } 
     else 
     { 
     delete_post_meta($post_id, $key); 
     foreach($value as $entry) add_post_meta($post_id, $key, $entry); 
     } 
     } 
    } 
    } 

add_action("manage_parts_custom_column", "part_custom_columns"); 
add_filter("manage_edit-parts_columns", "part_columns"); 

function part_columns($columns) 
    { 
    $columns = array(
    "cb" => "<input type=\"checkbox\" />", 
    "title" => "Name", 
    "weight" => "Weight", 
    "brand" => "Brand", 
    "price" => "Price" 
); 
    return $columns; 
    } 

function part_custom_columns($column) 
    { 
    global $post; 
    if ("ID" == $column) echo $post->ID; 
    elseif ("title" == $column) echo $post->post_title; 
    elseif ("weight" == $column) 
    { 
    $oweight = get_post_meta($post->ID, 'part-weight', true); 
    echo '<a href="' . $oweight . '" target="_blank">' . $oweight . '</a>'; 
    } 
    elseif ("brand" == $column) 
    { 
    $obrand = get_post_meta($post->ID, 'part-brand', true); 
    echo $obrand . '</a>'; 
    } 
    elseif ("price" == $column) 
    { 
    $oprice = get_post_meta($post->ID, 'part-price', true); 
    echo $oprice . '</a>'; 
    } 
    } 

?> 

也许我犯了一些错误?但是哪里?我使用WordPress 3.9.2

回答

1

'支持'=>阵列( '标题', '编辑', '缩略图', '自定义字段' )

它可以让你展现自定义帖子类型中的自定义字段选项。

+0

好,请让我知道如果您有任何问题。 – Tristup 2014-09-02 09:16:32

相关问题