如何在wp-admin中向自定义帖子类型添加元信息


How to add meta information to a custom post type in wp-admin?

我使用functions.php中的以下代码为wordpress网站设置了3种自定义帖子类型:

if ( function_exists('add_action') )
{
    add_action( 'init', 'create_post_types' );
    function create_post_types()
    {
        register_post_type('dj',Array('labels' => Array('name' => "DJ’s",'singular_name' => "DJ"),'public' => true,'has_archive' => true));
        register_post_type('gerecht',Array('labels' => Array('name' => "Gerechten",'singular_name' => "Gerecht"),'public' => true,'has_archive' => true));
        register_post_type('agenda',Array('labels' => Array('name' => "Agenda",'singular_name' => "Evenement"),'public' => true,'has_archive' => true));
    }
}

但在wp管理员屏幕上,我无法添加任何元信息到帖子中。我该怎么解决这个问题,还是根本不可能?

编辑:我不想使用任何插件,但我自己写代码。

您需要将supports字段添加到初始化数组中,如下所示:

register_post_type('dj', Array(
    'labels' => Array(
         'name' => "DJ’s",
         'singular_name' => "DJ"
     ),
    'public' => true,
    'has_archive' => true,
    'supports' => array('title', 'editor', 'custom-fields') // notice 'custom-fields'
));

默认情况下,只有标题编辑器,这就是为什么您可能无法在后端获得它们。

支持功能的完整列表如下:

  • title:创建文章标题的文本输入字段
  • editor:用于编写的内容输入框
  • comments:能够打开/关闭注释
  • trackbacks:能够打开/关闭trackbacks和pingbacks
  • 修订:允许对您的帖子进行修订
  • author:显示用于更改文章作者的选择框
  • 摘录:用于编写自定义摘录的文本区域
  • 缩略图:缩略图(3.0中的特色图像)上传框
  • 自定义字段:自定义字段输入区域
  • 页面属性:为页面显示的属性框。这是对于层次文章类型很重要,因此您可以选择父级职位

这里有一篇关于这个主题的好文章:WordPress中的自定义帖子类型。

这里还有一个更好的地方问WordPress相关的问题:WordPress的答案。

就我个人而言,我会使用名为Advanced Custom Fields的插件,它提供了一个很好的接口来实现这一点,因为它提供了广泛的选项。

您可以将以上内容与Custom Post Type UI结合使用,后者允许您使用UI创建自定义帖子类型和分类。仅供参考,您可以"获取代码"并将其放入您的函数.pp.

例如:

register_post_type('custom-post-name', array(  'label' => 'Custom Post Label','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => ''),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','custom-fields',),'labels' => array (
  'name' => 'Custom Post Name',
  'singular_name' => 'Value',
  'menu_name' => 'Custom Post Menu Name',
  'add_new' => 'Add Item',
  'add_new_item' => 'Add New Item',
  'edit' => 'Edit',
  'edit_item' => 'Edit Item',
  'new_item' => 'New Item',
  'view' => 'View Item',
  'view_item' => 'View Item',
  'search_items' => 'Search Custom Post',
  'not_found' => 'No Item(s) Found',
  'not_found_in_trash' => 'No Item(s) Found in Trash',
  'parent' => 'Parent Value',
),) );

您可能需要查看该数组并添加自己的描述性数据,即它对singular_namename表示Item