douluo2930 2014-09-01 08:26
浏览 60
已采纳

带metabox的自定义类型未显示其他字段

I have written such plugin in demo puproses, but why in admin panel i didn't see this additional fields? What i do wrong?

I made up custom data: car spare part, and there i must have two additional fields: weight, brand(taxonomy), price.

<?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/>' . "
";
  echo '<strong>Brand</strong><br/><br/><input name="part-brand" size="60" value="' . $my_fields['brand'] . '" /><br/><br/>' . "
";
  echo '<strong>Price</strong><br/><br/><input name="part-price" size="60" value="' . $my_fields['price'] . '" /><br/><br/>' . "
";
  }

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>';
    }
  }

?>

maybe i made some mistake? but where? i use wordpress 3.9.2

  • 写回答

1条回答 默认 最新

  • douchen1988 2014-09-01 09:18
    关注

    'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' )

    It will allow you to show the custom field option in custom post type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建