如何按父级-子级排序自定义分类法中的术语?
Got it, let's fix that taxonomy sorting so parents are followed directly by their children (and grandchildren, if you have them)! Since you already have code that prints terms, we just need to restructure the data into a nested tree first, then print it with proper hierarchy.
Step 1: Restructure Your Taxonomy into a Tree
First, we'll take your flat list of terms and build a nested structure where each parent has its children attached. Here's a practical example using PHP (adjust the syntax if you're using another language):
Let's assume your raw taxonomy data looks like this (adjust fields to match your actual data):
$taxonomyTerms = [ ['id' => 1, 'name' => 'Electronics', 'parent_id' => 0], ['id' => 2, 'name' => 'Laptops', 'parent_id' => 1], ['id' => 3, 'name' => 'Smartphones', 'parent_id' => 1], ['id' => 4, 'name' => 'Gaming Laptops', 'parent_id' => 2], ['id' => 5, 'name' => 'Home Appliances', 'parent_id' => 0], ['id' => 6, 'name' => 'Refrigerators', 'parent_id' => 5], ];
Now, use this function to convert the flat list into a hierarchical tree:
function buildHierarchicalTree($terms) { $termMap = []; $tree = []; // First, group all terms by their parent ID foreach ($terms as $term) { $termMap[$term['parent_id']][] = $term; } // Recursive function to build nested branches $buildBranch = function($parentId) use (&$buildBranch, &$termMap) { $branch = []; if (isset($termMap[$parentId])) { // Optional: Sort child terms by name (remove if you don't need this) usort($termMap[$parentId], function($a, $b) { return strcmp($a['name'], $b['name']); }); foreach ($termMap[$parentId] as $term) { // Attach children to the current term $term['children'] = $buildBranch($term['id']); $branch[] = $term; } } return $branch; }; // Start building from root terms (parent_id = 0, adjust if your root uses a different value) $tree = $buildBranch(0); return $tree; }
Step 2: Print the Hierarchical Tree
Now that we have a nested tree, we can print it with indentation to show the parent-child hierarchy:
function printTaxonomyHierarchy($tree, $depth = 0) { $indent = str_repeat(' ', $depth); // Adjust indentation as needed (tabs, spaces, etc.) foreach ($tree as $term) { // Print the current term with indentation to show level echo $indent . "- " . $term['name'] . "\n"; // Recursively print children if they exist if (!empty($term['children'])) { printTaxonomyHierarchy($term['children'], $depth + 1); } } } // Put it all together $sortedTaxonomy = buildHierarchicalTree($taxonomyTerms); printTaxonomyHierarchy($sortedTaxonomy);
What This Does
- The
buildHierarchicalTreefunction groups terms by their parent ID, then recursively attaches children to their parents to create a nested structure. - The optional
usortline sorts child terms alphabetically—remove it if you want to keep the original order, or adjust the comparator to sort by ID, date, etc. - The
printTaxonomyHierarchyfunction prints each term with indentation based on its depth, so parents are at the top, children are indented under them, and grandchildren are indented further, etc.
Example Output
Running the code above will give you this nicely structured output:
- Electronics - Laptops - Gaming Laptops - Smartphones - Home Appliances - Refrigerators
Adjustments for Your Code
- If your taxonomy uses objects instead of arrays, just change
$term['parent_id']to$term->parent_id(and adjust other field access accordingly). - If your root terms don't use
parent_id = 0, update the$buildBranch(0)call to match your root parent value (likenullor-1).
内容的提问来源于stack exchange,提问作者Hussain Mousavi

