admin管理员组

文章数量:1387426

CPT: product Taxonomy: prod_cat

Current URLs

  1. Child Term domain/product-category/pulp/
  2. Hierarchical Child Term domain/product-category/pulp-iqf/pulp/apple/
  3. Single CPT Post domain/product/apple-juice-concentrate/

Desired URLs

  1. Child Term domain/pulp/
  2. Hierarchical Child Term domain/pulp-iqf/pulp/apple/
  3. Single CPT Post domain/pulp-iqf/pulp/apple/apple-juice-concentrate/

I've gone through the depths of answers on WPSE and believe me I've tried various combinations but I'm unable to set it this way. The regex filled solutions around creating your own rules trips me and hence would really appreciate if someone can explain what can be done to either achieve the Desired URL structure or the way to achieve something close to the desired URLs.

//Register product post type
function products_post_type() {

        $labels = array(
            'name'                  => 'Products',
            'singular_name'         => 'Product',
            'menu_name'             => 'Products',
            'name_admin_bar'        => 'Post Type',
            'parent_item_colon'     => 'Parent Product:',
            'all_items'             => 'All Products',
            'add_new_item'          => 'Add New Product',
            'add_new'               => 'New Product',
            'new_item'              => 'New Item',
            'edit_item'             => 'Edit Product',
            'update_item'           => 'Update Product',
            'view_item'             => 'View Product',
            'search_items'          => 'Search products',
            'not_found'             => 'No products found',
            'not_found_in_trash'    => 'No products found in Trash',
            'items_list'            => 'Items list',
            'items_list_navigation' => 'Items list navigation',
            'filter_items_list'     => 'Filter items list',
        );
        $args = array(
            'label'                 => 'Product',
            'description'           => 'Products Post Type',
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,        
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'post',
            'rewrite'               => array(
                                        'slug' => 'product',
                                        'hierarchical' => true,
                                        'with_front' => false,
                                        )
        );
        register_post_type( 'product', $args );

    }
    add_action( 'init', 'products_post_type', 0 );

    // Register Product Category
    function prod_cat() {

        $labels = array(
            'name'                       => 'Product Categories',
            'singular_name'              => 'Product Category',
            'menu_name'                  => 'Product Category',
            'all_items'                  => 'All Items',
            'parent_item'                => 'Parent Item',
            'parent_item_colon'          => 'Parent Item:',
            'new_item_name'              => 'New Item Name',
            'add_new_item'               => 'Add New Item',
            'edit_item'                  => 'Edit Item',
            'update_item'                => 'Update Item',
            'view_item'                  => 'View Item',
            'separate_items_with_commas' => 'Separate items with commas',
            'add_or_remove_items'        => 'Add or remove items',
            'choose_from_most_used'      => 'Choose from the most used',
            'popular_items'              => 'Popular Items',
            'search_items'               => 'Search Items',
            'not_found'                  => 'Not Found',
            'items_list'                 => 'Items list',
            'items_list_navigation'      => 'Items list navigation',
        );
        $args = array(
            'labels'                        => $labels,
            'hierarchical'                  => true,
            'public'                        => true,
            'show_ui'                       => true,
            'show_admin_column'             => true,
            'show_in_nav_menus'             => true,
            'show_tagcloud'                 => true,
            'rewrite'                       => array(   'slug' => 'product-category',
                                                        'hierarchical' => true,
                                                        'with_front' => false, 
                                                    )  
        );
        register_taxonomy( 'prod_cat', array( 'product' ), $args );

    }
    add_action( 'init', 'prod_cat', 0 );

CPT: product Taxonomy: prod_cat

Current URLs

  1. Child Term domain/product-category/pulp/
  2. Hierarchical Child Term domain/product-category/pulp-iqf/pulp/apple/
  3. Single CPT Post domain/product/apple-juice-concentrate/

Desired URLs

  1. Child Term domain/pulp/
  2. Hierarchical Child Term domain/pulp-iqf/pulp/apple/
  3. Single CPT Post domain/pulp-iqf/pulp/apple/apple-juice-concentrate/

I've gone through the depths of answers on WPSE and believe me I've tried various combinations but I'm unable to set it this way. The regex filled solutions around creating your own rules trips me and hence would really appreciate if someone can explain what can be done to either achieve the Desired URL structure or the way to achieve something close to the desired URLs.

//Register product post type
function products_post_type() {

        $labels = array(
            'name'                  => 'Products',
            'singular_name'         => 'Product',
            'menu_name'             => 'Products',
            'name_admin_bar'        => 'Post Type',
            'parent_item_colon'     => 'Parent Product:',
            'all_items'             => 'All Products',
            'add_new_item'          => 'Add New Product',
            'add_new'               => 'New Product',
            'new_item'              => 'New Item',
            'edit_item'             => 'Edit Product',
            'update_item'           => 'Update Product',
            'view_item'             => 'View Product',
            'search_items'          => 'Search products',
            'not_found'             => 'No products found',
            'not_found_in_trash'    => 'No products found in Trash',
            'items_list'            => 'Items list',
            'items_list_navigation' => 'Items list navigation',
            'filter_items_list'     => 'Filter items list',
        );
        $args = array(
            'label'                 => 'Product',
            'description'           => 'Products Post Type',
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,        
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'post',
            'rewrite'               => array(
                                        'slug' => 'product',
                                        'hierarchical' => true,
                                        'with_front' => false,
                                        )
        );
        register_post_type( 'product', $args );

    }
    add_action( 'init', 'products_post_type', 0 );

    // Register Product Category
    function prod_cat() {

        $labels = array(
            'name'                       => 'Product Categories',
            'singular_name'              => 'Product Category',
            'menu_name'                  => 'Product Category',
            'all_items'                  => 'All Items',
            'parent_item'                => 'Parent Item',
            'parent_item_colon'          => 'Parent Item:',
            'new_item_name'              => 'New Item Name',
            'add_new_item'               => 'Add New Item',
            'edit_item'                  => 'Edit Item',
            'update_item'                => 'Update Item',
            'view_item'                  => 'View Item',
            'separate_items_with_commas' => 'Separate items with commas',
            'add_or_remove_items'        => 'Add or remove items',
            'choose_from_most_used'      => 'Choose from the most used',
            'popular_items'              => 'Popular Items',
            'search_items'               => 'Search Items',
            'not_found'                  => 'Not Found',
            'items_list'                 => 'Items list',
            'items_list_navigation'      => 'Items list navigation',
        );
        $args = array(
            'labels'                        => $labels,
            'hierarchical'                  => true,
            'public'                        => true,
            'show_ui'                       => true,
            'show_admin_column'             => true,
            'show_in_nav_menus'             => true,
            'show_tagcloud'                 => true,
            'rewrite'                       => array(   'slug' => 'product-category',
                                                        'hierarchical' => true,
                                                        'with_front' => false, 
                                                    )  
        );
        register_taxonomy( 'prod_cat', array( 'product' ), $args );

    }
    add_action( 'init', 'prod_cat', 0 );
Share Improve this question asked Aug 24, 2017 at 10:33 vajrasarvajrasar 1693 silver badges19 bronze badges 1
  • What you desire can't be done by changing the options passed to register_post_type or register_taxonomy, it will require a custom rewrite rule, which means regular expressions are unavoidable – Tom J Nowell Commented Aug 28, 2017 at 20:44
Add a comment  | 

1 Answer 1

Reset to default 0

CPT does't have much options for building custom permalinks. What you need can't be done without custom coding and creating new rewrite rules, and handling permalink parsing.

I have plugin for working with post types and taxonomies, and it includes options for custom permalinks and more. It is not a free plugin, but in many aspects is unique, and you can check it out via my profile home page.

本文标签: Custom Permalinks with CPT and Hierarchical Taxonomies