【问题标题】:How add a custom field in a custom post type如何在自定义帖子类型中添加自定义字段
【发布时间】:2012-08-02 11:45:09
【问题描述】:

我想在我的自定义帖子类型中添加一个自定义字段。我想在自定义帖子类型中添加一个文本字段,但我不知道该怎么做。这是我在主题的functions.php中实现的代码。

add_action( 'init', 'register_cpt_advert' );

function register_cpt_advert() {

$labels = array( 
    'name' => _x( 'Adverts', 'advert' ),
    'singular_name' => _x( 'Advert', 'advert' ),
    'add_new' => _x( 'Add New', 'advert' ),
    'add_new_item' => _x( 'Add New Advert', 'advert' ),
    'edit_item' => _x( 'Edit Advert', 'advert' ),
    'new_item' => _x( 'New Advert', 'advert' ),
    'view_item' => _x( 'View Advert', 'advert' ),
    'search_items' => _x( 'Search Adverts', 'advert' ),
    'not_found' => _x( 'No advert found', 'advert' ),
    'not_found_in_trash' => _x( 'No adverts found in Trash', 'advert' ),
    'parent_item_colon' => _x( 'Parent Advert:', 'advert' ),
    'menu_name' => _x( 'Adverts', 'advert' ),
);

$args = array( 
    'labels' => $labels,
    'hierarchical' => false,
    'description' => 'Show image adverts on videos',
    'supports' => array( 'title', 'thumbnail'),
    //'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
    'public' => false,
    'show_ui' => true,
    'show_in_menu' => true,


    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => true,
    'has_archive' => false,
    'query_var' => false,
    'can_export' => true,
    'rewrite' => false,
    'capability_type' => 'post'
);
register_post_type( 'advert', $args );
}

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    这个插件对于在任何帖子类型上添加自定义字段非常有用。

    http://wordpress.org/extend/plugins/more-fields/

    【讨论】:

      【解决方案2】:

      如果您想要特定帖子类型的自定义字段,Meta Box 也是一个很棒的插件。

      http://www.deluxeblogtips.com/meta-box/

      【讨论】:

        【解决方案3】:

        你可以使用Advanced Custom Fields插件 在使用 CPT UI 来创建自定义 Pot Type 时使用这个

        如果您使用Generator 生成插件,请转到此参考 https://sltaylor.co.uk/blog/control-your-own-wordpress-custom-fields/

        【讨论】:

          猜你喜欢
          • 2012-07-29
          • 1970-01-01
          • 2015-01-11
          • 1970-01-01
          • 2011-07-01
          • 2021-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多