1

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

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-admin 屏幕中,我无法在帖子中添加任何元信息。我该如何解决这个问题,或者这是不可能的?

编辑:我不想为此使用任何插件,而是自己编写代码。

4

2 回答 2

1

就个人而言,我会使用名为Advanced Custom Fields的插件,它提供了非常好的界面来执行此操作,因为它提供了广泛的选项。

您可以将上述内容与Custom Post Type UI结合使用,它允许您使用 UI 创建自定义帖子类型和分类。仅供参考,您可以“获取代码”并将其放入您的 functions.php 中。

一个例子:

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',
),) );

您可能想查看数组并添加您自己的描述性数据,即它所说Itemsingular_namename

于 2012-08-28T14:24:08.150 回答
1

您需要在初始化数组中添加支持字段,如下所示:

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

默认情况下它只有titleeditor,这就是为什么你可能不会在后端得到它们。

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

  • title : 用于创建帖子标题的文本输入字段。
  • editor : 用于写作的内容输入框。
  • 评论:能够打开/关闭评论。
  • trackbacks:能够打开/关闭trackbacks和pingbacks。
  • 修订:允许对您的帖子进行修订。
  • author:显示一个用于更改帖子作者的选择框。
  • excerpt:用于编写自定义摘录的文本区域。
  • thumbnail : 缩略图(3.0 中的特色图片)上传框。
  • custom-fields:自定义字段输入区域。
  • page-attributes:为页面显示的属性框。这对于分层帖子类型很重要,因此您可以选择父帖子。

这是关于该主题的一篇不错的文章:WordPress 中的自定义帖子类型

这里也是询问 WordPress 相关问题的更好地方:WordPress Answers

于 2012-08-28T14:41:47.410 回答