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

时间:2012-08-02 11:45:10

标签: php wordpress 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 );
}

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

Place this code just below the code of your custom post type 

//code start
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {
//give name of your input field
    add_meta_box( 'my-meta-box-id', 'Enter your input field title', 'cd_meta_box_cb', 'Adverts', 'normal', 'high' );
}
function cd_meta_box_cb( $post ) {
    $values = get_post_custom( $post->ID );
    $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>

        <label for="my_meta_box_text">Title</label>
        <input type="text" name="my_meta_box_text" maxlength="55" id="my_meta_box_text" value="<?php echo $text; ?>" style="width: 30%;" />
    </p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id ) {
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post', $post_id ) ) return;
    // now we can actually save the data
    $allowed = array(
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    // Probably a good idea to make sure your data is set
    if( isset( $_POST['my_meta_box_text'] ) )
        update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
//code end

答案 3 :(得分:0)

您可以使用Advanced Custom Fields插件 当使用CPT UI创建自定义Pot类型

时使用此选项

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