感谢任何帮助谢谢伊恩 解决方案 我为在 Rest Api 中显示做了类似的事情,所以所做的只是将所有类别排列成分层数组/对象.而且只对一年级的孩子有效.$allcats = get_categories();//获取所有类别//$allcats = get_the_category();//只获取分配给帖子的类别$parents = $all_ids = array();//找到父母foreach ($allcats 作为 $cats) {如果($cats->category_parent === 0){$cats->children = array();$parents[] = $cats;}}//找到 childredn 并将其分配给相应的父级foreach ($allcats 作为 $cats) {如果 ($cats->category_parent != 0) {foreach ($parents 作为 $parent) {if ($cats->category_parent === $parent->term_id) {$parent->children[] = $cats;}}}}所以结果将类似于:[{term_id: 50,name: "主猫",弹头:主猫",term_group: 0,term_taxonomy_id: 50,分类:类别",描述: "",父母:0,计数:77,过滤器:原始",term_order: "1",cat_ID: 50,类别计数:77,类别描述:"",cat_name: "主猫",category_nicename: "main_cat",category_parent: 0,孩子们: [{term_id: 49,name: "子猫",slug: "子猫",term_group: 0,term_taxonomy_id: 49,分类:类别",描述: "",父母:50,计数:43,过滤器:原始",term_order: "1",cat_ID: 49,类别计数:43,类别描述:"",cat_name: "sub_cat",category_nicename: "子猫",category_parent:50},{等其他孩子}]}]希望这对某人有所帮助...I have seen a few questions for how to display parent and child categories but can't seem to find a solution for what I am doing.I'm using the default wordpress post with several categories and child categories for an example I have News- Travel- World- BusinessArticle-GeneralCase Study-Thomas Cook- Easy JetThe post can have any number of categories but what I am looking to do is group the child with its parent so that when I display a post it, for example, displays the Parent and Child categories used for each post in the following format (colon after the parent and hyphen after each child apart from the last).News: Travel - WorldCurrently I am using the following code;<?php $categories = get_the_category();$separator = ' - ';$output = '';if ( ! empty( $categories ) ) { foreach( $categories as $category ) { $output .= '<a class="blog-panel-cat" href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator; } echo trim( $output );}?>But his just outputs one category after the other with no way of grouping so I can add ':' after the parent and separate the child for that parent with '-' and also a double space before the next parent.Any help appreciatedThanksIan 解决方案 I have done something similar for showing in Rest Api, so all this does is arranges all of the categories into hierarchical arrays/objects. And it only works for first-grade children. $allcats = get_categories(); // get ALL categories // $allcats = get_the_category(); // get only categories assigned to post$parents = $all_ids = array();// find parentsforeach ($allcats as $cats) { if ($cats->category_parent === 0 ) { $cats->children = array(); $parents[] = $cats; }}// find childredn and assign it to corresponding parrent foreach ($allcats as $cats) { if ($cats->category_parent != 0) { foreach ($parents as $parent) { if ($cats->category_parent === $parent->term_id) { $parent->children[] = $cats; } } }}So the result will be something like:[{ term_id: 50, name: "Main Cat", slug: "main-cat", term_group: 0, term_taxonomy_id: 50, taxonomy: "category", description: "", parent: 0, count: 77, filter: "raw", term_order: "1", cat_ID: 50, category_count: 77, category_description: "", cat_name: "main cat", category_nicename: "main_cat", category_parent: 0, children: [ { term_id: 49, name: "Sub Cat", slug: "sub-cat", term_group: 0, term_taxonomy_id: 49, taxonomy: "category", description: "", parent: 50, count: 43, filter: "raw", term_order: "1", cat_ID: 49, category_count: 43, category_description: "", cat_name: "sub_cat", category_nicename: "Sub Cat", category_parent: 50 }, {etc other children} ] }]Hope this helps someone... 这篇关于将父子类别分组以进行发布和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 03:10