我知道这是一个老问题,但这可能对其他人有帮助……</p>
要完成您的目标,您需要使用register_post_type和register_taxonomy WordPress 函数。
您的 functions.php 文件中需要类似的内容
// Register Custom Post Type
function products() {
$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'menu_name' => 'Product',
'parent_item_colon' => 'Parent Product:',
'all_items' => 'All Products',
'view_item' => 'View Product',
'add_new_item' => 'Add New Product',
'add_new' => 'New Product',
'edit_item' => 'Edit Product',
'update_item' => 'Update Product',
'search_items' => 'Search products',
'not_found' => 'No products found',
'not_found_in_trash' => 'No products found in Trash'
);
$args = array(
'label' => 'product',
'description' => 'Product information pages',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', ),
'taxonomies' => array( 'category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post'
);
register_post_type( 'product', $args );
}
// Hook into the 'init' action
add_action( 'init', 'products', 0 );
// Register Custom Taxonomy
function product-category() {
$labels = array(
'name' => 'Product Category',
'singular_name' => 'Product Categories',
'menu_name' => 'Product Category',
'all_items' => 'All Product Categories',
'parent_item' => 'Parent Product Category',
'parent_item_colon' => 'Parent Product Category:',
'new_item_name' => 'New Product Category Name',
'add_new_item' => 'Add New Product Category',
'edit_item' => 'Edit Product Category',
'update_item' => 'Update Product Category',
'separate_items_with_commas' => 'Separate product categories with commas',
'search_items' => 'Search product categories',
'add_or_remove_items' => 'Add or remove product categories',
'choose_from_most_used' => 'Choose from the most used product categories',
);
$rewrite = array(
'slug' => 'product-category',
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => 'product-category',
'rewrite' => $rewrite,
);
register_taxonomy( 'product-category', 'product', $args );
}
// Hook into the 'init' action
add_action( 'init', 'product-category', 0 );
您可以使用http://generatewp.com/post-type/之类的自定义帖子生成器,让您在处理所有选项时更轻松。
一旦您注册了自定义帖子类型和分类法以在添加此代码或对 URL 结构进行任何修改后刷新 DNS 规则,这一点很重要。通过访问 WordPress 管理员设置 -> 永久链接页面来执行此操作,它会自动刷新 DNS 规则。
编辑主题文件夹中的以下文件以控制显示的模板:
- 存档-product.php
- 分类-产品-category.php
- 单品.php