0

WordPress テーマを作成していて、カスタム投稿タイプを追加したいと考えています。私はオンラインで見ましたが、答えが見つからないようです。

今、私はこのコードを functions.php に持っています

add_action( 'init', 'register_cpt_tutorial' );

function register_cpt_tutorial() {

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

$args = array( 
    'labels' => $labels,
    'hierarchical' => false,
    'description' => 'Tutorials description will be here',
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
    'taxonomies' => array( 'category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 20,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);

register_post_type( 'tutorial', $args );
}

このコードをコピーして貼り付け、「チュートリアル」を「ビデオ」に変更すると、2 つ目の投稿タイプが作成されません。誰か助けてくれませんか?

4

1 に答える 1

1

WP プラグインの Custom Post Type UI を使用することをお勧めします

http://wordpress.org/plugins/custom-post-type-ui/

しかし、コードを使用したい場合は、これを試してください:

add_action( 'init', 'register_cpt_tutorial' );

function register_cpt_tutorial() {

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

$args = array( 
    'labels' => $labels,
    'hierarchical' => false,
    'description' => 'Tutorials description will be here',
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
    'taxonomies' => array( 'category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 20,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);
// ----- Second post type
$labels2 = array( 
    'name' => _x( 'video', 'video' ),
    'singular_name' => _x( 'video', 'video' ),
    'add_new' => _x( 'Add New', 'video' ),
    'add_new_item' => _x( 'Add New video', 'video' ),
    'edit_item' => _x( 'Edit video', 'video' ),
    'new_item' => _x( 'New video', 'video' ),
    'view_item' => _x( 'View video', 'video' ),
    'search_items' => _x( 'Search video', 'video' ),
    'not_found' => _x( 'No video found', 'video' ),
    'not_found_in_trash' => _x( 'No video found in Trash', 'video' ),
    'parent_item_colon' => _x( 'Parent video:', 'tutorial' ),
    'menu_name' => _x( 'video', 'video' ),
);

$args2 = array( 
    'labels' => $labels2,
    'hierarchical' => false,
    'description' => 'Tutorials description will be here',
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
    'taxonomies' => array( 'category' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 20,

    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);

register_post_type( 'tutorial', $args );
register_post_type( 'video', $args2 );
}

$args - $args2 と $labels - $labels2 の変更に注意してください

于 2013-11-02T01:46:11.153 に答える