从自定义分类生成 WordPress 菜单

发布于 2024-11-11 16:47:41 字数 3019 浏览 0 评论 0原文

我创建了一个具有一些自定义分类法的自定义帖子类型(产品)。其中之一是“类别”。我希望在“产品”下使用自定义分类“类别”自动生成菜单,以便他们可以在菜单上单击“产品 ->”,它将把他们带到该类别的特定产品的列表(我已经有一个显示单个产品的页面和一个列出所有产品的页面)。请注意,某些category分类法将有子分类法,我也想在菜单中显示它。

我对 WordPress 有点陌生,我知道如何创建菜单的唯一方法是通过 wp-admin,但我不想进入并为每个类别和子类别创建页面/菜单。

我所说的有可能吗?谢谢你!

自定义帖子类型在这里:

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

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('productcategory', 'product', array (
        'label' => __('Product Category'),
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-category'),
        )); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2024-11-18 16:47:41

尝试wp_list_categories。它从分类法中导出类别,并链接到您选择的深度。 (您想要多少个子类别)格式化为列表(例如使用

  • 元素)。
    我会在模板中您想要替换的菜单中添加此内容。希望这有帮助,但如果没有帮助,我会回来并更具体。
    我忘了提及它链接到类别页面的页面。请参阅此处。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文