本文介绍了如何在 WordPress 中获取自定义分类法的所有父/子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个自定义 WordPress 主题,我想显示所有分类列表
例如,如果我想要这个结构:
父类别 1
- 儿童类别 1
- 儿童类别 2
- 儿童类别 3
1.1 - 孙子类别 1
父类别 2
- 儿童类别 4
- 儿童类别 5
2.1.- 大孩子类别 2
你们能帮我解决这个难题吗
解决方案
要创建此结构,使用辅助数组可能是解决方案.这不是一个 100% 的解决方案,它只会让您启动,您可以从这里开始,因为单独解决它会更有帮助.
$all_terms = array();$taxonomy = '类别';$parent_args = ['分类' =>$分类法,'父母' =>0,'hide_empty' =>错误的];$parent_terms = get_terms( $parent_args );foreach ( $parent_terms 作为 $parent_term ) {$all_terms[ $parent_term->term_id ] = get_all_term_children( $parent_term, $taxonomy );}函数 get_all_term_children( $term, $taxonomy ){if ( is_wp_error( get_term_children( $term->term_id, $taxonomy ) ) ) {返回;}返回 get_term_children( $term->term_id, $taxonomy );}
I’m working on a custom WordPress theme and I want to show all the taxonomies list
So, for example, if I want this structure:
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
Parent Category 2
- Child Category 4
- Child Category 5
can you guys please help me to solve this puzzle
解决方案
To create this structure, maybe going with a helper array is the solution. This is not a 100% solution it will just give you start up and you can go from here since figuring it out all alone will be more helpful.
$all_terms = array();
$taxonomy = 'category';
$parent_args = [
'taxonomy' => $taxonomy,
'parent' => 0,
'hide_empty' => false
];
$parent_terms = get_terms( $parent_args );
foreach ( $parent_terms as $parent_term ) {
$all_terms[ $parent_term->term_id ] = get_all_term_children( $parent_term, $taxonomy );
}
function get_all_term_children( $term, $taxonomy ){
if ( is_wp_error( get_term_children( $term->term_id, $taxonomy ) ) ) {
return;
}
return get_term_children( $term->term_id, $taxonomy );
}
这篇关于如何在 WordPress 中获取自定义分类法的所有父/子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!