简体   繁体   中英

Wordpress URL Rewrite to sync custom taxonomy and post type

Here are my current URLs for my custom post type and taxonomy. I noticed the rewrite slug for the custom post type and taxonomy cannot both be "inventory" or it breaks. So i went with "inventory-category" and "inventory".

current urls:

category http://localhost:3000/inventory-category/actual-category-name/

single http://localhost:3000/inventory/product-title

I want:

category http://localhost:3000/inventory/actual-category-name/

single http://localhost:3000/inventory/actual-category-name/product-title

So I have 2 questions

  1. How do I get the post type and taxonomy to both share the same url "inventory" instead of using "inventory-category".

  2. How do I get the single to use the actual category name? So it could be:

http://localhost:3000/inventory/cars/product-title

http://localhost:3000/inventory/trucks/product-title

// register a new list of categories for custom post type
function so_inventory_categories() {
    $args = array(
        'hierarchical'       => true,
        'show_ui'            => true,
        'show_admin_column'  => true,
        'query_var'          => true,
        'public'             => true,
        'publicly_queryable' => true,
        'rewrite'            => array('with_front' => false, 'slug' => 'inventory-category'),
    );

    register_taxonomy( 'inventory_categories', array( 'inventory' ), $args );
}
add_action( 'init', 'so_inventory_categories' );

function so_posttype_inventory() {
    $args = array(
        'labels'              => ['name'=>'Inventory'],
        'supports'            => array( 'title', 'editor' ),
        'taxonomies'          => array( 'inventory_categories' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => array('with_front' => false,'slug' => 'inventory'),
        'menu_icon'           => 'dashicons-plus-alt',
    );
    register_post_type( 'inventory', $args );
}

add_action( 'init', 'so_posttype_inventory', 0 );

You should use rewrite rules to make that work. Something like this:

add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
    add_rewrite_rule('^inventory/([^/]+)/?$', 'index.php?taxonomy=inventory_category&inventory_category=$matches[1]', 'top');
    add_rewrite_rule('^inventory/([^/]+)/([^/]+)/?$', 'index.php?post_type=inventory&inventory=$matches[2]', 'top');
}

I haven't tested this so it might still need some tweaking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM