簡體   English   中英

如何創建自定義分類法,我可以為該特定分類法創建 select 類別

[英]How to create custom taxonomy where I can select category for that particular taxonomy

我需要為自定義分類法添加類別選擇。

例如,我有兩個名為“板球和足球”的類別。 我有一個叫做播放器的自定義分類法。 當我當時添加像“abc”這樣的球員時,我需要 select 板球或足球等運動。

我如何在 WordPress 中管理它?

您需要做的就是在 arguments 中為您的自定義帖子類型添加此行:

'taxonomies' => array( 'category' ),

您的自定義帖子類型的現有代碼中可能已經包含此行,其中包含一些其他自定義分類法。 如果這樣做,那么您只需要在其后添加一個逗號並添加一個類別,如下所示:

'taxonomies' => array('other_tax', 'category' ),

這是一個完整的代碼示例,用於創建一個名為“播放器”的自定義帖子類型,支持內置類別。

    function custom_post_type() {
     
        $labels = array(
            'name'                => 'Players',
            'singular_name'       => 'Player',
            'menu_name'           => 'Players',
            'parent_item_colon'   => 'Parent Player',
            'all_items'           => 'All Players',
            'view_item'           => 'View Player',
            'add_new_item'        => 'Add New Player',
            'add_new'             => 'Add New',
            'edit_item'           => 'Edit Player',,
            'update_item'         => 'Update Player',
            'search_items'        => 'Search Player',
            'not_found'           => 'Not Found',
            'not_found_in_trash'  => 'Not found in Trash',
        );
         
    // Set other options for Custom Post Type
         
        $args = array(
            'label'               => 'player',
            'description'         => 'sports people',
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'menu_icon'           => 'dashicons-universal-access',
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'page',
            'show_in_rest'        => true,
             
            // This is where we add taxonomies to the CPT
            'taxonomies'          => array( 'category' ),
        );
         
        // Registering your Custom Post Type
        register_post_type( 'player', $args );
     
    }

 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'custom_post_type', 0 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM