簡體   English   中英

如何顯示帶有子類別的 WordPress 自定義帖子類型類別(分類法)循環

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

我正在研究具有多級產品類別的自定義產品列表的項目,如果用戶單擊 1 個類別/分類法,它將轉到其子類別,然后它將顯示其所有帖子列表 (CPT)。 我創建了“我們的產品”的自定義帖子類型 這是我創建的 CPT 代碼:

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

下面也是產品CPT分類的代碼:

//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' );

以下是您可以查看的參考站點: https : //www.nasserieequipment.com/products-boom-loader-forklift-17.html

請讓我知道我們如何獲得具有多級層次結構的類別列表

你可以用編程方式做到這一點

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

}

你應該有兩個 foreach 循環。 第一個用於獲取父分類術語,第二個用於獲取子分類術語。

在第二個 foreach 中,您需要指定父分類術語 ID,即第一個 foreach 循環中的 $parent_term->term_id。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM