我在演示傀儡中写过这样的插件,但为什么在管理面板中我没有看到这个额外的字段?我做错了什么?
我制作了自定义数据:汽车备件,我必须有两个额外的领域:重量,品牌(分类),价格。
<?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
答案 0 :(得分:1)
&#39;支持&#39; =&GT;阵列( &#39;标题&#39 ;, &#39;编辑&#39 ;, &#39;缩略图&#39 ;, &#39;自定义字段&#39; )
它允许您在自定义帖子类型中显示自定义字段选项。