带有类别的自定义帖子类型会导致错误
I don't know what I'm doing wrong. I have a custom post type called models
. Within this custom post type I register also a custom taxonomy. This custom taxonomy is used for categories. But I getting every time a 404 error.
This is how I create my custom post type:
$labels = array(
'name' => __('Modellen', 'models'),
'singular_name' => __('Model', 'models'),
'add_new' => __('Nieuwe toevoegen', 'models'),
'add_new_item' => __('Nieuw model', 'models'),
'edit_item' => __('Model bewerken', 'models'),
'new_item' => __('Nieuw model', 'models'),
'view_item' => __('Model bekijken', 'models'),
'search_items' => __('Model zoeken', 'models'),
'not_found' => __('Niks gevonden', 'models'),
'not_found_in_trash' => __('Niks gevonden in de prullenbak', 'models'),
'parent_item_colon' => __('Model', 'models'),
'menu_name' => __('Modellen', 'models'),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array('title', 'page-attributes', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions'),
//'taxonomies' => array('category'), // Old
//'taxonomies' => array('category'), // New not used any more
'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' => false,
'capability_type' => 'post',
);
register_post_type('models', $args);
//register_taxonomy_for_object_type('category', 'models'); // Old
//register_taxonomy_for_object_type('category', 'models'); // New not used any more
This is how I create my custom taxonomy:
$labels = array(
'name' => _x('Categorieën', 'taxonomy general name', 'textdomain'),
'singular_name' => _x('Categorie', 'taxonomy singular name', 'textdomain'),
'search_items' => __('Zoek Categorieën', 'textdomain'),
'all_items' => __('All Categorieën', 'textdomain'),
'parent_item' => __('Hoofd categorie', 'textdomain'),
'parent_item_colon' => __('Hoofd categorie:', 'textdomain'),
'edit_item' => __('Bewerk categorie', 'textdomain'),
'update_item' => __('Update Categorie', 'textdomain'),
'add_new_item' => __('Nieuwe categorie', 'textdomain'),
'new_item_name' => __('Nieuwe categorie naam', 'textdomain' ),
'menu_name' => __('Categorieën', 'textdomain'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => false,
'show_tagcloud' => false,
'show_in_quick_edit' => true,
'show_admin_column' => false,
'rewrite' => array('slug' => 'models', 'hierarchical' => true),
);
//register_taxonomy('category', array('models'), $args); // Old
register_taxonomy('cat_models', array('models'), $args); // New
Thanks!
Edit:
Maybe good to mention:
- I execute the code inside my own custom plugin.
- I use the default Twenyseventeen theme
- WordPress 4.8
Solution: Thanks to @AmitJoshi I needed to change the category to something else. Above the updated code with the changes.
Yeah you were trying to register a custom taxonomy with the reserved term category
. That's why it was not working.
Often when adding new rewrites (such as you might do upon adding new taxonomies in a plugin or theme), you need to refresh the rewrite rules. If you don't, you'll run into 404 errors when trying to access the new taxonomy or custom post type.
The easiest way to rebuild/refresh the rewrites is to go to Settings > Permalinks and click the "Save Changes" button. You don't have to make any changes, just hit save.
The solution hanks to @AmitJoshi below the updated code.
This is how I create my custom post type:
$labels = array(
'name' => __('Modellen', 'models'),
'singular_name' => __('Model', 'models'),
'add_new' => __('Nieuwe toevoegen', 'models'),
'add_new_item' => __('Nieuw model', 'models'),
'edit_item' => __('Model bewerken', 'models'),
'new_item' => __('Nieuw model', 'models'),
'view_item' => __('Model bekijken', 'models'),
'search_items' => __('Model zoeken', 'models'),
'not_found' => __('Niks gevonden', 'models'),
'not_found_in_trash' => __('Niks gevonden in de prullenbak', 'models'),
'parent_item_colon' => __('Model', 'models'),
'menu_name' => __('Modellen', 'models'),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array('title', 'page-attributes', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions'),
'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' => false,
'capability_type' => 'post',
);
register_post_type('models', $args);
This is how I create my custom taxonomy:
$labels = array(
'name' => _x('Categorieën', 'taxonomy general name', 'textdomain'),
'singular_name' => _x('Categorie', 'taxonomy singular name', 'textdomain'),
'search_items' => __('Zoek Categorieën', 'textdomain'),
'all_items' => __('All Categorieën', 'textdomain'),
'parent_item' => __('Hoofd categorie', 'textdomain'),
'parent_item_colon' => __('Hoofd categorie:', 'textdomain'),
'edit_item' => __('Bewerk categorie', 'textdomain'),
'update_item' => __('Update Categorie', 'textdomain'),
'add_new_item' => __('Nieuwe categorie', 'textdomain'),
'new_item_name' => __('Nieuwe categorie naam', 'textdomain' ),
'menu_name' => __('Categorieën', 'textdomain'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => false,
'show_tagcloud' => false,
'show_in_quick_edit' => true,
'show_admin_column' => false,
'rewrite' => array('slug' => 'models', 'hierarchical' => true),
);
register_taxonomy('cat_models', array('models'), $args);