我已经创建了2个Wordpress自定义帖子类型
一个称为类别,另一个称为产品,产品以类别为父级,代码如下:
产品
function my_custom_post_product() {
$labels = array(
\'name\' => _x( \'Products\', \'post type general name\' ),
\'singular_name\' => _x( \'Product\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => __( \'Add New Product\' ),
\'edit_item\' => __( \'Edit Product\' ),
\'new_item\' => __( \'New Product\' ),
\'all_items\' => __( \'All Products\' ),
\'view_item\' => __( \'View Product\' ),
\'search_items\' => __( \'Search Products\' ),
\'not_found\' => __( \'No products found\' ),
\'not_found_in_trash\' => __( \'No products found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Products\'
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'description\' => \'Holds our products and product specific data\',
\'public\' => true,
\'publicly_queryable\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\',\'page-attributes\' ),
\'has_archive\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'\',
\'with_front\' => false,
)
);
register_post_type( \'product\', $args );
}
add_action( \'init\', \'my_custom_post_product\' );
类别
function my_custom_post_category() {
$labels = array(
\'name\' => _x( \'Categories\', \'post type general name\' ),
\'singular_name\' => _x( \'Category\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => __( \'Add New Category\' ),
\'edit_item\' => __( \'Edit Category\' ),
\'new_item\' => __( \'New Category\' ),
\'all_items\' => __( \'All Categories\' ),
\'view_item\' => __( \'View Category\' ),
\'hierarchical\' => true,
\'search_items\' => __( \'Search Categories\' ),
\'not_found\' => __( \'No categories found\' ),
\'not_found_in_trash\' => __( \'No categories found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Categories\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Category Pages\',
\'hierarchical\' => true,
\'public\' => true,
\'publicly_queryable\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\',\'post-formats\',\'custom-fields\',\'page-attributes\' ),
\'has_archive\' => true,
\'can_export\' => true,
\'query_var\' => \'category\',
);
register_post_type( \'category\', $args );
}
add_action( \'init\', \'my_custom_post_category\' );
我遇到的问题是permalinks,目前他们是这样的:
类别-http://domain.com/category/category-name/产品-http://domain.com/product/category-name/product-name/
我想这样做:
类别-http://domain.com/category-name/产品-http://domain.com/category-name/product-name/
我尝试了各种方法,但即使在刷新permalink设置后,最终还是得到了404。
SO网友:benny-ben
我不建议使用类别作为cpt尝试重命名它,例如在product type中。有关类别的更多信息,请参阅codex:Taxonomies
function my_custom_post_product_type() {
$labels = array(
\'name\' => _x( \'Product Types\' ),
\'singular_name\' => _x( \'Product Type\' ),
\'add_new\' => _x( \'Add New\' ),
\'add_new_item\' => __( \'Add New Product Type\' ),
\'edit_item\' => __( \'Edit Product Type\' ),
\'new_item\' => __( \'New Product Type\' ),
\'all_items\' => __( \'All Product Types\' ),
\'view_item\' => __( \'View Product Type\' ),
\'hierarchical\' => true,
\'search_items\' => __( \'Search Product Types\' ),
\'not_found\' => __( \'No Product Types found\' ),
\'not_found_in_trash\' => __( \'No Product Types found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Product Types\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Product Type Pages\',
\'hierarchical\' => true,
\'public\' => true,
\'publicly_queryable\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\',\'post-formats\',\'custom-fields\',\'page-attributes\' ),
\'has_archive\' => true,
\'can_export\' => true,
\'query_var\' => \'product-type\'
);
register_post_type( \'product-type\', $args );
}
add_action( \'init\', \'my_custom_post_product_type\' );