我定义了以下自定义帖子类型+分类法
$productTaxonomyLabels = [
\'name\' => __(\'Product Categories\', \'byronposttypes\'),
\'singular_name\' => __(\'Product Category\', \'byronposttypes\'),
];
$productTaxonomyArgs = [
\'labels\' => $productTaxonomyLabels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_admin_column\' => true,
\'description\' => __(\'Product category\', \'byronposttypes\'),
\'hierarchical\' => true,
\'rewrite\' => [
\'slug\' => __(\'collections\', \'byronposttypes\'),
\'with_front\' => false,
\'hierarchical\' => true,
\'ep_mask\' => EP_CATEGORIES
],
];
register_taxonomy(\'collections\', \'products\', $productTaxonomyArgs);
register_taxonomy_for_object_type(\'collections\', \'products\');
$productLabels = [
\'name\' => _x(\'Products\', \'post type plural name\', \'byronposttypes\'),
\'singular_name\' => _x(\'Product\', \'post type singular name\', \'byronposttypes\'),
\'menu_name\' => _x(\'Collection\', \'name in admin menu\', \'byronposttypes\'),
\'add_new\' => _x(\'Add new\', \'product\', \'byronposttypes\'),
\'add_new_item\' => __(\'Add new product\', \'byronposttypes\'),
\'new_item\' => __(\'New product\', \'byronposttypes\'),
\'edit_item\' => __(\'Edit product\', \'byronposttypes\'),
\'view_item\' => __(\'View product\', \'byronposttypes\'),
\'not_found\' => __(\'No products found\', \'byronposttypes\'),
\'not_found_in_trash\' => __(\'No products found in trash\', \'byronposttypes\'),
\'all_items\' => __(\'All products\', \'byronposttypes\'),
\'archives\' => __(\'Product archives\', \'byronposttypes\'),
\'attributes\' => __(\'Product attributes\', \'byronposttypes\'),
];
$productSupports = [\'title\', \'editor\', \'thumbnail\'];
$productArgs = [
\'labels\' => $productLabels,
\'description\' => __(\'Products\', \'byronposttypes\'),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'menu_position\' => 57,
\'menu_icon\' => \'dashicons-screenoptions\',
\'capability_type\' => [\'product\', \'products\'],
\'hierarchical\' => false,
\'supports\' => $productSupports,
\'taxonomies\' => [\'banaan\'],
\'rewrite\' => [
\'slug\' => \'banaan\',
\'with_front\' => false,
],
\'has_archive\' => \'banaan\',
];
register_post_type(\'products\', $productArgs);
如您所见,我已将post类型slug定义为“banaan”。问题是wordpress生成的链接(例如,在wp admin中单击“查看”时,uri为
collection/$product_name
. 我以前使用/collection作为url的基础,但出于测试目的,我将其更改为banaan,但它不会改变。我重新保存了permalinks。我错过什么了吗
*手动导航到/banaan/$product\\u name会重定向到/collection/$product\\u name并给出404
最合适的回答,由SO网友:Connor 整理而成
我似乎已经解决了这个问题。我将我的CPT从products重命名为product
$productTaxonomyLabels = [
\'name\' => __(\'Product Categories\', \'byronposttypes\'),
\'singular_name\' => __(\'Product Category\', \'byronposttypes\'),
];
$productTaxonomyArgs = [
\'labels\' => $productTaxonomyLabels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_admin_column\' => true,
\'description\' => __(\'Product category\', \'byronposttypes\'),
\'hierarchical\' => true,
\'rewrite\' => [
\'slug\' => __(\'collections\', \'byronposttypes\'),
\'with_front\' => false,
\'hierarchical\' => true,
\'ep_mask\' => EP_CATEGORIES
],
];
register_taxonomy(\'collections\', \'product\', $productTaxonomyArgs);
register_taxonomy_for_object_type(\'collections\', \'product\');
$productLabels = [
\'name\' => _x(\'Products\', \'post type plural name\', \'byronposttypes\'),
\'singular_name\' => _x(\'Product\', \'post type singular name\', \'byronposttypes\'),
\'menu_name\' => _x(\'Collection\', \'name in admin menu\', \'byronposttypes\'),
\'add_new\' => _x(\'Add new\', \'product\', \'byronposttypes\'),
\'add_new_item\' => __(\'Add new product\', \'byronposttypes\'),
\'new_item\' => __(\'New product\', \'byronposttypes\'),
\'edit_item\' => __(\'Edit product\', \'byronposttypes\'),
\'view_item\' => __(\'View product\', \'byronposttypes\'),
\'not_found\' => __(\'No products found\', \'byronposttypes\'),
\'not_found_in_trash\' => __(\'No products found in trash\', \'byronposttypes\'),
\'all_items\' => __(\'All products\', \'byronposttypes\'),
\'archives\' => __(\'Product archives\', \'byronposttypes\'),
\'attributes\' => __(\'Product attributes\', \'byronposttypes\'),
];
$productSupports = [\'title\', \'editor\', \'thumbnail\'];
$productArgs = [
\'labels\' => $productLabels,
\'description\' => __(\'Products\', \'byronposttypes\'),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'menu_position\' => 57,
\'menu_icon\' => \'dashicons-screenoptions\',
\'capability_type\' => [\'product\', \'products\'],
\'hierarchical\' => false,
\'supports\' => $productSupports,
\'taxonomies\' => [\'collections\'],
\'rewrite\' => [
\'slug\' => __(\'product\', \'byronposttypes\'),
\'with_front\' => false,
\'ep_mask\' => EP_PERMALINK
],
\'has_archive\' => false,
];
register_post_type(\'product\', $productArgs);
我仍然不知道是什么导致了这个问题,但更改CPT的名称似乎可以解决这个问题。