admin管理员组文章数量:1287082
Been trying do this and still no luck. What I'm trying is to list taxonomy terms assigned to a post ordered by parent-child category relationship (hierarchical view).
Structure is something like this
Library = custom post type
Year = custom taxonomy assigned to "library"
- 2021 = parent term
-- April = child term
-- May = child term
-- June = child term
-- July = child term
The desired output is:
2021, April, May, June, July
I tried:
$terms = get_the_terms( $post->ID, 'document_year' );
if ( $terms && ! is_wp_error( $terms ) ) :
$output = array();
foreach ( $terms as $term ) {
$output[] = $term->name;
}
echo '<span>' . implode ( ', ', $output ) . '</span>';
endif;
But this outputs an alphabetical order of terms.
Same with this:
echo ( get_the_term_list( $post->ID, 'category', '', ' / ') );
Any help please?
Been trying do this and still no luck. What I'm trying is to list taxonomy terms assigned to a post ordered by parent-child category relationship (hierarchical view).
Structure is something like this
Library = custom post type
Year = custom taxonomy assigned to "library"
- 2021 = parent term
-- April = child term
-- May = child term
-- June = child term
-- July = child term
The desired output is:
2021, April, May, June, July
I tried:
$terms = get_the_terms( $post->ID, 'document_year' );
if ( $terms && ! is_wp_error( $terms ) ) :
$output = array();
foreach ( $terms as $term ) {
$output[] = $term->name;
}
echo '<span>' . implode ( ', ', $output ) . '</span>';
endif;
But this outputs an alphabetical order of terms.
Same with this:
echo ( get_the_term_list( $post->ID, 'category', '', ' / ') );
Any help please?
Share Improve this question edited Sep 22, 2021 at 12:40 Ioannis Koukotzilas asked Sep 22, 2021 at 12:32 Ioannis KoukotzilasIoannis Koukotzilas 11 bronze badge1 Answer
Reset to default 0If you have a need to build up an actual structure of terms, this might be a useful method for you:
/**
* Recursively sort an array of taxonomy terms hierarchically. Child categories will be
* placed under a 'children' member of their parent term.
* @param Array $cats taxonomy term objects to sort
* @param Array $into result array to put them in
* @param integer $parentId the current parent ID to put them in
*/
function sort_terms_hierarchically(Array &$cats, Array &$into, $parentId = 0)
{
foreach ($cats as $i => $cat) {
if ($cat->parent == $parentId) {
$into[$cat->term_id] = $cat;
unset($cats[$i]);
}
}
foreach ($into as $topCat) {
$topCat->children = array();
sort_terms_hierarchically($cats, $topCat->children, $topCat->term_id);
}
}
Usage is as follows:
$categories = get_terms('my_taxonomy_name', array('hide_empty' => false));
$categoryHierarchy = array();
sort_terms_hierarchically($categories, $categoryHierarchy);
var_dump($categoryHierarchy);
本文标签: categoriesList taxonomy terms assigned to a post in hierarchical view
版权声明:本文标题:categories - List taxonomy terms assigned to a post in hierarchical view 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741306011a2371372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论