url rewrite for custom taxonomy
-
Hi,
I’ve got a custom post type ‘news’ as well as a custom taxonomy ‘news-cat’ (category)
The only reason I called it news-cat (and not ‘news’) as it’d clash with ‘news’ post type. At the moment the urls are as follows:
All posts of type ‘news’: domain.com/news/
A single post of type ‘news’: domain.com/news/my-news-articleWe have a number of terms within the news-cat taxonomy (eg. family, trips, etc.)
If I query a certain term to get all news of certain term: eg. family, the url is domain.com/news-cat/family/
Is it possible to rewrite it somehow so that wordpress won’t confuse a taxonomy term and a single news item. Ideally, we’d like to have:
domain.com/news/family/ for a taxonomy term query
domain.com/news/single-news-article for a single post of a custom type ‘news’function news_taxonomy() { $labels = array( 'name' => _x( 'News Category', 'Taxonomy General Name' ), 'singular_name' => _x( 'News Category', 'Taxonomy Singular Name' ), 'search_items' => __( 'Search News Category' ), 'all_items' => __( 'All News Categories' ), 'parent_item' => __( 'Parent News Category' ), 'parent_item_colon' => __( 'Parent News Category:' ), 'edit_item' => __( 'Edit News Category' ), 'update_item' => __( 'Update News Category' ), 'add_new_item' => __( 'Add News Category' ), 'new_item_name' => __( 'New News Category' ), 'menu_name' => __( 'News Category' ), ); $args = array( 'hierarchical' => true, // like categories or tags 'labels' => $labels, 'show_in_rest' => true, 'show_ui' => true, // add the default UI to this taxonomy 'show_admin_column'=> true, // add the taxonomies to the wordpress admin 'query_var' => true, 'rewrite' => array( 'slug' => 'news-cat'), ); register_taxonomy( 'news_tax', 'news', $args ); }
register_post_type('news', array( 'has_archive' => true, 'rewrite' => array('slug' => 'news'), 'supports' => array('title', 'editor', 'excerpt', 'thumbnail'), 'public' => true, 'show_in_rest' => true, 'labels' => array( 'name' => 'News', 'add_new_item' => 'Add News', 'edit_item' => 'Edit News', 'all_items' => 'All News', 'singular_name' => 'News' ), 'menu_icon' => 'dashicons-calendar-alt' ));
Please advise.
- The topic ‘url rewrite for custom taxonomy’ is closed to new replies.