Ant*_*nne 1 hierarchy taxonomy custom-post-type custom-taxonomy
我想按层次结构获得一篇文章的所有分类(循环)。示例我有这些分类法,以及括号中的税收 ID。
Tax1(1)
-Tax2(3)
--Tax3(2)
Run Code Online (Sandbox Code Playgroud)
我想将它们收集起来,也许按照这个顺序排列成一个数组。现在我设法得到了这 3 个的数组,但顺序是错误的。我无法通过 id 来订购,因为一开始并没有订购 ID。我也无法通过名称和名称来订购它。(我当前的分类法名称不是 Tax1、Tax2...)
我现在的代码是
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
Run Code Online (Sandbox Code Playgroud)
使用“Wordpress” Walker类创建分类法的层次结构
<?php
class Walker_Quickstart extends Walker {
// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'parent',
'id' => 'term_id'
);
/**
* At the start of each element, output a <p> tag structure.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<p>%s %s (%s)</p>\n",
str_repeat('‐', $depth),
$item->name,
$item->term_id
);
}
}?>
Run Code Online (Sandbox Code Playgroud)
该类将创建元素的层次结构。将此类与返回的元素一起使用,如下所示:
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
$walk = new Walker_Quickstart();
echo $walk->walk($productcategories, 0);
Run Code Online (Sandbox Code Playgroud)