简体   繁体   中英

How to show WordPress custom post type categories (Taxonomy) loop with sub-categories

I am working on project of having custom product list with having multi-level categories of products, if user clicks on 1 category/taxonomy it will go to its sub-category & then it will show all the list of its posts (CPT). I've created custom post type of 'Our Products' This is the CPT code I've created:

// Register Custom Post Type Our Product
function create_ourproduct_cpt() {
$labels = array(
    'name' => _x( 'Our Products', 'Post Type General Name', 'our-products' ),
    'singular_name' => _x( 'Our Product', 'Post Type Singular Name', 'our-products' ),
    'menu_name' => _x( 'Our Products', 'Admin Menu text', 'our-products' ),
    'name_admin_bar' => _x( 'Our Product', 'Add New on Toolbar', 'our-products' ),
    'archives' => __( 'Our Product Archives', 'our-products' ),
    'attributes' => __( 'Our Product Attributes', 'our-products' ),
    'parent_item_colon' => __( 'Parent Our Product:', 'our-products' ),
    'all_items' => __( 'All Our Products', 'our-products' ),
    'add_new_item' => __( 'Add New Our Product', 'our-products' ),
    'add_new' => __( 'Add New', 'our-products' ),
    'new_item' => __( 'New Our Product', 'our-products' ),
    'edit_item' => __( 'Edit Our Product', 'our-products' ),
    'update_item' => __( 'Update Our Product', 'our-products' ),
    'view_item' => __( 'View Our Product', 'our-products' ),
    'view_items' => __( 'View Our Products', 'our-products' ),
    'search_items' => __( 'Search Our Product', 'our-products' ),
    'not_found' => __( 'Not found', 'our-products' ),
    'not_found_in_trash' => __( 'Not found in Trash', 'our-products' ),
    'featured_image' => __( 'Featured Image', 'our-products' ),
    'set_featured_image' => __( 'Set featured image', 'our-products' ),
    'remove_featured_image' => __( 'Remove featured image', 'our-products' ),
    'use_featured_image' => __( 'Use as featured image', 'our-products' ),
    'insert_into_item' => __( 'Insert into Our Product', 'our-products' ),
    'uploaded_to_this_item' => __( 'Uploaded to this Our Product', 'our-products' ),
    'items_list' => __( 'Our Products list', 'our-products' ),
    'items_list_navigation' => __( 'Our Products list navigation', 'our-products' ),
    'filter_items_list' => __( 'Filter Our Products list', 'our-products' ),
);
$args = array(
    'label' => __( 'Our Product', 'our-products' ),
    'description' => __( 'Our Products Section', 'our-products' ),
    'labels' => $labels,
    'menu_icon' => 'dashicons-grid-view',
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'author', 'comments', 'trackbacks', 'page-attributes', 'post-formats', 'custom-fields'),
    'taxonomies' => array(),
            //'taxonomies' => array('categories'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => true,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type( 'ourproduct', $args );
}
add_action( 'init', 'create_ourproduct_cpt', 0 );

And below is also the code for product CPT taxonomy:

//our product category taxonomy
function tr_create_my_taxonomy() {
    register_taxonomy(
        'ourproduct-category',
        'ourproduct',
        array(
            'label' => __( 'Prod Category' ),
            'rewrite' => array( 'slug' => 'prod-category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );

Below is the reference site you can check: https://www.nasseriequipment.com/products-boom-loader-forklift-17.html

Please let me know how we can get the list of categories with its multi-level hierarchy

You can do it programmatically with

foreach( get_terms( 'ourproduct-category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) { 
  echo $parent_term->name . '<br>';

  foreach( get_terms( 'ourproduct-category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term ) {    
    echo $child_term->name . '<br>';
  }

}

You should have two foreach loops. One for getting parent taxonomy terms, and second for getting child taxonomy terms.

In the second foreach you need to specify the parent taxonomy term ID which is $parent_term->term_id from the first foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM