如何在自定义帖子类型中添加自定义字段


How add a custom field in a custom post type

我想在我的自定义帖子类型中添加一个自定义字段。我想在自定义帖子类型中添加一个文本字段,但我不知道如何做到这一点。以下是我在主题中的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 );
}

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

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

如果你想在特定的帖子类型上自定义字段,Meta-Box也是一个很好的插件。

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

您可以使用高级自定义字段插件在使用CPT UI创建自定义锅型时使用此选项

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