开发者

Generating wordpress menu from custom taxonomies

开发者 https://www.devze.com 2023-03-11 10:02 出处:网络
I\'ve created a custom post type (products) that has some custom taxonomies.One of which is \'category\'.I would like to have my menu under Products generated automatically with the custom taxonomy \'

I've created a custom post type (products) that has some custom taxonomies. One of which is 'category'. I would like to have my menu under Products generated automatically with the custom taxonomy 'category' so that on the menu they can click PRODUCTS -> <category> and it will take them to a list of that particular products with that category (I've already got a page that displays a single product, and a page that lists ALL the products). Note that some category taxonomies will have children and I would like to show that in the menu too.

I'm somewhat new to wordpress and the only way I know how to create the menu is through the wp-admin, but I don't want to go in and create a page/menu for each category and subcategory.

Is what I'm talking about even possible? Thank you!

Custom Post Type is here:

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);  
}

And here is the taxonomy:

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('p开发者_开发知识库roductcategory', 'product', array (
        'label' => __('Product Category'),
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-category'),
        )); 
}


Try wp_list_categories. It exports the categories from a taxonomy with links to the depth of your choosing. (so as many children categories as you want) formatted as a list (e.g. with <ul>and <li> elements).
I would add this where the menu you want to replace would go in your template. Hope that helps, but I'll come back and be more specific if it doesn't.
I forgot to mention that the pages that it links to go to category pages. See here.

0

精彩评论

暂无评论...
验证码 换一张
取 消