Custom Post Type with Custom Taxonomy – Permalink issue

Question

Given the situation where:

Taxonomy page (taxonomy-x.php):

/news/category-name

Template page (template-x.php):

/news

I’m currently using CPT UI – how can (is it even possible?) to have the following:

Template page:

/news

Taxonomy page:

/category-name

I can use the rewrite; although one or the other will break. – meaning I would like to have the taxonomy page top level, not under /news/taxonomy.

News Post Type:

function cptui_register_my_cpts_news() {

    /**
     * Post Type: News.
     */

    $labels = array(
        "name" => __( "News", "custom-post-type-ui" ),
        "singular_name" => __( "News", "custom-post-type-ui" ),
        "menu_name" => __( "News", "custom-post-type-ui" ),
        "all_items" => __( "All News", "custom-post-type-ui" ),
        "add_new" => __( "Add new news", "custom-post-type-ui" ),
        "add_new_item" => __( "Add new news", "custom-post-type-ui" ),
        "edit_item" => __( "Edit news", "custom-post-type-ui" ),
        "new_item" => __( "New news", "custom-post-type-ui" ),
        "view_item" => __( "View news", "custom-post-type-ui" ),
        "view_items" => __( "View news", "custom-post-type-ui" ),
        "search_items" => __( "Search for news", "custom-post-type-ui" ),
        "not_found" => __( "No news found", "custom-post-type-ui" ),
    );

    $args = array(
        "label" => __( "News", "custom-post-type-ui" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "delete_with_user" => false,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "news", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail", "excerpt", "trackbacks", "custom-fields", "comments", "revisions", "author", "page-attributes", "post-formats" ),
        "taxonomies" => array( "news_categories" ),
    );

    register_post_type( "news", $args );
}

add_action( 'init', 'cptui_register_my_cpts_news' );

News Categories taxonomy:

function cptui_register_my_taxes_news_categories() {

    /**
     * Taxonomy: News Categories.
     */

    $labels = array(
        "name" => __( "News Categories", "custom-post-type-ui" ),
        "singular_name" => __( "News Category", "custom-post-type-ui" ),
    );

    $args = array(
        "label" => __( "News Categories", "custom-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'news_categories', 'with_front' => true, ),
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "news_categories",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        );
    register_taxonomy( "news_categories", array( "news" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_news_categories' );

News page (template-news.php) – /news which gets all the custom post type posts and shows them as cards.

0
, Julian Camilleri 4 years 2019-10-25T09:05:13-05:00 0 Answers 123 views 0

Leave an answer

Browse
Browse