这是我修改的代码https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
/*
* Creating a function to create our CPT
* just drop this in functions.php inside your theme file basically...
* You replaced all \'twentytwenty\' with \'twentyseventeen\'
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
\'name\' => _x( \'Products\', \'Post Type General Name\', \'twentyseventeen\' ),
\'singular_name\' => _x( \'Product\', \'Post Type Singular Name\', \'twentyseventeen\' ),
\'menu_name\' => __( \'Products\', \'twentyseventeen\' ),
\'parent_item_colon\' => __( \'Parent Product\', \'twentyseventeen\' ),
\'all_items\' => __( \'All Products\', \'twentyseventeen\' ),
\'view_item\' => __( \'View Product\', \'twentyseventeen\' ),
\'add_new_item\' => __( \'Add New Product\', \'twentyseventeen\' ),
\'add_new\' => __( \'Add New\', \'twentyseventeen\' ),
\'edit_item\' => __( \'Edit Product\', \'twentyseventeen\' ),
\'update_item\' => __( \'Update Product\', \'twentyseventeen\' ),
\'search_items\' => __( \'Search Product\', \'twentyseventeen\' ),
\'not_found\' => __( \'Not Found\', \'twentyseventeen\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'twentyseventeen\' ),
);
// Set other options for Custom Post Type
$args = array(
//\'label\' => __( \'products\', \'twentyseventeen\' ),
\'description\' => __( \'Products and reviews\', \'twentyseventeen\' ),
\'labels\' => $labels,
// Features this CPT supports in Post Editor
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
\'taxonomies\' => array( \'category\', \'tag\' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
\'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\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
\'show_in_rest\' => true,
);
// Registering your Custom Post Type
register_post_type( \'products\', $args );
}
/* Hook into the \'init\' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( \'init\', \'custom_post_type\', 0 );
我创建了一个名为“shop”的类别,我希望这些CPT“产品”显示在我的“shop”类别中,以便您访问该域时。你会看到他们的列表。
我在分类法中添加了“类别”功能,并且我可以在创建新产品时选择商店类别,但永久链接始终是唯一的域。com/产品/产品名称
每当我创建新的产品自定义帖子时,我都会填写描述、添加自定义字段、标题、选择店铺类别。。。在我按publish之前一切都很好。。。然后编辑器中会出现一个“permalink”下拉部分,我被告知可以在域中看到该产品。com/产品/产品名称。。。当我从未选择产品类别时。。。而且产品类别甚至不存在/从未存在!!!
有什么好处?
如果我访问域。商店里什么也看不见。
如果我访问域。com/shop/product name我被重定向到域。com/products/product name(顺便说一下,它显示得很好)。
你知道这个被称为“产品”的幽灵类别是从哪里来的吗?
为什么编辑不尊重我将产品列入“商店”类别的愿望?而是将其放在自己的“产品”类别中(在wordpress管理仪表板中甚至不存在/从未创建)。
我对任何php代码所做的唯一编辑就是在我的主题函数中添加单个函数。php文件。
我甚至更新/刷新了permalinks设置,方法是在仪表板的自定义permalinks部分重新保存。
非常感谢。
编辑:我想我可能错过了“重写”参数?如下所述:https://developer.wordpress.org/reference/functions/register_post_type/
\'rewrite\'
(bool|array) Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be passed with any of these keys: