admin管理员组

文章数量:1391987

I have a custom pos type created in functions.php and working great - except I need to have categories enabled. I have looked through other answers here, and am still not able to figure it out. In my code i have taxonomies => array('categories', 'tags') but it is not working.

Here is my code:

function custom_post_type() {

    // Set UI labels for Custom Post Type
    $labels = array(
        'name' => _x('Trade Alerts', 'Post Type General Name', 'Avada'),
        'singular_name' => _x('Trade Alert', 'Post Type Singular Name', 'Avada'),
        'menu_name' => __('Trade Alerts', 'Avada'),
        'parent_item_colon' => __('Parent Trade Alert', 'Avada'),
        'all_items' => __('All Trade Alerts', 'Avada'),
        'view_item' => __('View Trade Alert', 'Avada'),
        'add_new_item' => __('Add New Trade Alert', 'Avada'),
        'add_new' => __('Add New', 'Avada'),
        'edit_item' => __('Edit Trade Alert', 'Avada'),
        'update_item' => __('Update Trade Alert', 'Avada'),
        'search_items' => __('Search Trade Alert', 'Avada'),
        'not_found' => __('Not Found', 'Avada'),
        'not_found_in_trash' => __('Not found in Trash', 'Avada'),
    );

    // Set other options for Custom Post Type

    $args = array(
        'label' => __('trade alerts', 'Avada'),
        'description' => __('Trade alerts with Forex stuff', 'Avada'),
        'labels' => $labels,
        // Features this CPT supports in Post Editor
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies' => array('categories', 'tags'),
        /* A hierarchical CPT is like Pages and can have
         * Parent and child items. A non-hierarchical CPT
         * is like Posts.
         */
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );

I have a custom pos type created in functions.php and working great - except I need to have categories enabled. I have looked through other answers here, and am still not able to figure it out. In my code i have taxonomies => array('categories', 'tags') but it is not working.

Here is my code:

function custom_post_type() {

    // Set UI labels for Custom Post Type
    $labels = array(
        'name' => _x('Trade Alerts', 'Post Type General Name', 'Avada'),
        'singular_name' => _x('Trade Alert', 'Post Type Singular Name', 'Avada'),
        'menu_name' => __('Trade Alerts', 'Avada'),
        'parent_item_colon' => __('Parent Trade Alert', 'Avada'),
        'all_items' => __('All Trade Alerts', 'Avada'),
        'view_item' => __('View Trade Alert', 'Avada'),
        'add_new_item' => __('Add New Trade Alert', 'Avada'),
        'add_new' => __('Add New', 'Avada'),
        'edit_item' => __('Edit Trade Alert', 'Avada'),
        'update_item' => __('Update Trade Alert', 'Avada'),
        'search_items' => __('Search Trade Alert', 'Avada'),
        'not_found' => __('Not Found', 'Avada'),
        'not_found_in_trash' => __('Not found in Trash', 'Avada'),
    );

    // Set other options for Custom Post Type

    $args = array(
        'label' => __('trade alerts', 'Avada'),
        'description' => __('Trade alerts with Forex stuff', 'Avada'),
        'labels' => $labels,
        // Features this CPT supports in Post Editor
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies' => array('categories', 'tags'),
        /* A hierarchical CPT is like Pages and can have
         * Parent and child items. A non-hierarchical CPT
         * is like Posts.
         */
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );
Share Improve this question asked Mar 16, 2020 at 20:22 Ben BlueBen Blue 1171 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Your code is basically correct, except that the standard taxonomies have singular names.

Use taxonomies' => array('category', 'post_tag') instead.

You should be able to relate category taxonomy to your custom post type with register_taxonomy_for_object_type()

add_action('init','add_categories_to_cpt');
function add_categories_to_cpt(){
    register_taxonomy_for_object_type('category', 'post_type_name');
}

本文标签: taxonomyHow do i enable categories for my custom post type