WordPress使用的URL与自定义帖子类型的重写参数中定义的URL不同

时间:2017-04-06 作者:Connor

我定义了以下自定义帖子类型+分类法

$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

2 个回复
最合适的回答,由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的名称似乎可以解决这个问题。

SO网友:rudtek

尝试将CPT中的“重写”设置为:

\'rewrite\' => false,
保存永久链接<检查现场正面,确保其正常工作。(如果没有,您可能在某处有重复的代码…)

然后将其设置回:

\'rewrite\' => (
    \'slug\' => \'banaan\',
    \'with_front\' => false,
),
再次冲洗permalinks<微笑(希望如此)

为什么选择使用括号而不是括号?您可以尝试使用传统方法。

相关推荐