How to hook custom taxonomies to custom post types and make the permalinks work?

So,

I have my custom post type with the slug /news. And I want my category view to be /news/category/CATEGORYID.

Read More

For that I create my custom taxonomy:

add_action('init', 'create_allfilmnews_categories', 0);
function create_allfilmnews_categories() {
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search categories' ),
        'all_items' => __( 'All category' ),
        'parent_item' => __( 'Parent category' ),
        'parent_item_colon' => __( 'Parent category:' ),
        'edit_item' => __( 'Edit category' ),
        'update_item' => __( 'Update category' ),
        'add_new_item' => __( 'Add new category' ),
        'new_item_name' => __( 'New category name' ),
        'menu_name' => __( 'News categories' ),
    );  

    register_taxonomy('news_category', 'news', array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'category' ),
    ));
}

I was hoping, that since it is hooked to news custom post-type.. and its slug is “category”, so I was hoping that naturally it would work out like I wanted (/news/category/CATEGORYID). And use my custom category-template just by creating a file to the template directory: category-news_category.php.

However, of course it is not working.

Is it even possible to have a custom taxonomy aka. categories inside custom post-type, that work perfectly with permalinks?

Related posts

Leave a Reply

1 comment

  1. Two things – I think the category URL would be: yourdomain.com/news_category/NEWS-CATEGORY-SLUG-HERE

    So for example if you created a category “top headlines” in your “news_category” taxonomy… it would look like this (assuming the slug for “top headlines” is “top-headlines”):

    /news_category/top-headlines/

    The second thing…
    I think your template name should be “taxonomy-news_category.php”

    (edited to add – I don’t think you can have the slug be “category” as that is being used by WordPress already.. so that may be causing a conflict in itself…I could be wrong on that though…)