【问题标题】:How to add meta information to a custom post type in wp-admin?如何将元信息添加到 wp-admin 中的自定义帖子类型?
【发布时间】:2012-08-28 14:22:15
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    您需要在初始化数组中添加 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'
    ));
    

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

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

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

    这是一篇关于该主题的精彩文章:Custom post types in WordPress

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

    【讨论】:

      【解决方案2】:

      就我个人而言,我会使用名为 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',
      ),) );
      

      您可能想要查看该数组并添加您自己的描述性数据,即在 Item 表示 singular_namename 的位置。

      【讨论】:

      • 我不想使用插件。所以你的代码看起来很棒,我想这就是它的全部规则:'supports' => array('title','editor','custom-fields',) 对吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      相关资源
      最近更新 更多