因此,我需要显示一个页面,其中包含当前自定义分类法的子类别,以及它们各自的帖子。
我需要的是,当我单击sub-cat链接时,我会得到如下结果:
\'Tipos\' (custom taxonomy)
- Auto (first sub-cat)
- Sub-Item 1 (first sub-sub-cat)
- Post 1
- Post 2
- Sub-Item 2 (second sub-sub-cat)
- Post 1
- Post 2
我有另一只sub-cat,但我不想看到它们,因为我点击了sub-cat“auto”内的sub-sub-cat。
自定义帖子类型的名称为“Produtos”
自定义分类法的名称为“Tipos”
我使用的页面是“分类tipos”。php
我知道我需要:
获取当前自定义分类法创建一个循环,显示该自定义分类法的子类别列表在子类别列表内,显示每个子类别内的帖子我了解WordPress的层次结构,但问题是我需要根据分类法自动显示这些子类别,而我无法实现这一点。
通过我的代码,我获得了自定义分类法中的所有类别。我只想得到当前版本中的版本。
This is the code I tried:
Removed the code so it wont confuse anyone
你们能帮我点个火吗?谢谢
PROBLEM SOLVED.
FINAL CODE (WORKING):
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
echo $term->name;
$queried_object = get_queried_object();
$term_id = get_queried_object()->term_id;
$taxonomyName = "tipos";
$termchildren = get_term_children( $term_id, $taxonomyName );
if ($termchildren != false){
foreach ($termchildren as $child) {
$term2 = get_term_by( \'id\', $child, $taxonomyName );
echo $term2->name;
$my_query = new WP_Query( array(
\'post_type\' => \'produtos\',
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomyName,
\'field\' => \'slug\',
\'terms\' => $term2->slug,
)
),
));
while ($my_query->have_posts()) : $my_query->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}
} else{
$my_query2 = new WP_Query( array(
\'post_type\' => \'produtos\',
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomyName,
\'field\' => \'slug\',
\'terms\' => $term->slug,
)
),
));
while ($my_query2->have_posts()) : $my_query2->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}
最合适的回答,由SO网友:Marcelo Henriques Cortez 整理而成
PROBLEM SOLVED.
FINAL CODE (WORKING):
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
echo $term->name;
$queried_object = get_queried_object();
$term_id = get_queried_object()->term_id;
$taxonomyName = "tipos";
$termchildren = get_term_children( $term_id, $taxonomyName );
if ($termchildren != false){
foreach ($termchildren as $child) {
$term2 = get_term_by( \'id\', $child, $taxonomyName );
echo $term2->name;
$my_query = new WP_Query( array(
\'post_type\' => \'produtos\',
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomyName,
\'field\' => \'slug\',
\'terms\' => $term2->slug,
)
),
));
while ($my_query->have_posts()) : $my_query->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}
} else{
$my_query2 = new WP_Query( array(
\'post_type\' => \'produtos\',
\'posts_per_page\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomyName,
\'field\' => \'slug\',
\'terms\' => $term->slug,
)
),
));
while ($my_query2->have_posts()) : $my_query2->the_post();
//content you want to show
endwhile;
wp_reset_postdata();
}