dongxi1965 2017-08-03 19:16
浏览 30
已采纳

带有类别的自定义帖子类型会导致错误

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.

  • 写回答

3条回答 默认 最新

  • douyue7408 2017-08-03 19:52
    关注

    Yeah you were trying to register a custom taxonomy with the reserved term category. That's why it was not working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘