我创建了一个具有一些自定义分类法的自定义帖子类型(产品)。其中之一是"类别"。我希望使用自定义分类法"category"自动生成"Products"下的菜单,这样他们就可以在菜单上单击PRODUCTS -> <category>
,它会将他们带到该类别的特定产品列表中(我已经有一个显示单个产品的页面,还有一个列出所有产品的页面)。请注意,一些category
分类法会有子分类法,我也想在菜单中显示这一点。
我对wordpress有点陌生,我知道如何创建菜单的唯一方法是通过wp管理员,但我不想为每个类别和子类别创建页面/菜单。
我所说的可能吗?非常感谢。
自定义邮件类型在这里:
add_action( 'init', 'create_product_post_type' );
function create_product_post_type()
{
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'product'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_item' => __('Search Products'),
'not_found' => __('No products found'),
'not_found_in_trash' => __('No products found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Products'
);
$args = array(
'label' => __('Products'),
'labels' => $labels,
'public' => true,
'can_export' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'menu_icon' => get_bloginfo('template_url').'/functions/images/product.png',
'hierarchical' => false,
'rewrite' => array( "slug" => "product" ),
'supports' => array('title'), //MAYBE add thumbnail later!
'show_in_nav_menus' => true
);
register_post_type( 'product', $args);
}
下面是分类法:
function create_productcategory_taxonomy() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Add New Product Category' ),
'new_item_name' => __( 'New Product Category' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove product categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' )
);
register_taxonomy('productcategory', 'product', array (
'label' => __('Product Category'),
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-category'),
));
}
尝试wp_list_categories。它从分类法中导出类别,并链接到您选择的深度。(根据需要提供任意多个子类别)格式化为列表(例如,具有<ul>
和<li>
元素)
我会把它添加到你想要替换的菜单在你的模板中的位置。希望这有帮助,但如果没有,我会回来更具体地说
我忘了提一下它链接到类别页面的页面。请参见此处。