我想你可以用get_terms()
为了这个。这只是一些简单的最小代码,我想您还需要添加永久链接、术语描述或其他内容。
然而,我刚刚测试了这段代码(现在使用分类法“course”进行了测试),它可以正常工作。
我会尽最大努力对代码进行注释:
// our current taxonomy slug
// If you want to get the current taxonomy automatically try using $wp_query->get_queried_object();
$taxonomy = \'course\';
// we get the terms of the taxonomy \'course\', but only top-level-terms with (parent => 0)
$top_level_terms = get_terms( array(
\'taxonomy\' => $taxonomy,
\'parent\' => \'0\',
\'hide_empty\' => false,
) );
// only if some terms actually exists, we move on
if ($top_level_terms) {
echo \'<ul class="top-level-terms">\';
foreach ($top_level_terms as $top_level_term) {
// the id of the top-level-term, we need this further down
$top_term_id = $top_level_term->term_id;
// the name of the top-level-term
$top_term_name = $top_level_term->name;
// the current used taxonomy
$top_term_tax = $top_level_term->taxonomy;
// note that the closing </li> is set further down, so that we can add a sub list item correctly
echo \'<li class="top-level-term"><strong>\'.$top_term_name.\'</strong>\';
// here we get the child-child terms
// for this we are using \'child_of\' => $top_term_id
// I also set \'parent\' => $top_term_id here, with this line you will only see this level and no further childs
$second_level_terms = get_terms( array(
\'taxonomy\' => $top_term_tax, // you could also use $taxonomy as defined in the first lines
\'child_of\' => $top_term_id,
\'parent\' => $top_term_id, // disable this line to see more child elements (child-child-child-terms)
\'hide_empty\' => false,
) );
// start a second list element if we have second level terms
if ($second_level_terms) {
echo \'<ul class="second-level-terms">\';
foreach ($second_level_terms as $second_level_term) {
$second_term_name = $second_level_term->name;
echo \'<li class="second-level-term">\'.$second_term_name.\'</li>\';
}// END foreach
echo \'</ul><!-- END .second-level-terms -->\';
}// END if
echo \'</li><!-- END .top-level-term -->\';
}// END foreach
echo \'</ul><!-- END .top-level-terms -->\';
}// END if
还可以尝试使用
print_r($top_level_term);
例如,在第一个foreach循环中。这样,你就可以看到一个术语附带的所有信息。
我想说的是,您应该在常规分类法归档模板中使用此代码,而不要使用该文件taxonomy-course-photoshop.php
. 因为那样的话,每个术语都需要一个单独的模板文件,这是不可行的。
Edit:此外,确保您有一些职位分配给单个条款和子条款。否则,将根本不显示术语!
<小时>Update:是的,您可以自动获取当前分类法和术语数据。
如果你在里面工作taxonomy-course.php
然后您可以尝试以下操作:
//get the current object
$current = $wp_query->get_queried_object();
// try var_dump($current); to see all available data!
// return the ID of the current term
// i.e. ID of term "Photoshop" is "26", so we get "26" if we are viewing "Photoshop"
$current_term_id = $current->term_id;
// return the nicename of the current term
// i.e. returns "Photoshop"
// or "ps-thematic#1" if we are on a child term of "Photoshop"
$current_name = $current->name;
// returns the current taxonomy slug we are in
// i.e. it will return "course"
$current_taxonomy = $current->taxonomy;
// returns the ID of the parent, if we have a parent
// i.e. if we are viewing "ps-thematic#1" it will return the ID of "Photoshop", 26
// if we are viewing "Photoshop", it will return 0, because "Photoshop" is a top level term
$current_parent = $current->parent;
// display name of current term, i.e. "Photoshop"
echo \'<strong>\'.$current_name.\'</strong>\';
$sub_terms = get_terms( array(
\'taxonomy\' => $current_taxonomy,
\'child_of\' => $current_term_id,
\'hide_empty\' => false,
) );
// only start if some sub terms exist
if ($sub_terms) {
// try var_dump($sub_terms); to see all available data!
echo \'<ul class="sub-terms">\';
foreach ($sub_terms as $sub_term) {
// try var_dump($sub_term); to see all available data!
// only show the name for the example, "ps-thematic#1"
echo \'<li>\'.$sub_term->name.\'</li>\';
}// END foreach
echo \'</ul><!-- END .sub-terms -->\';
}// END if
因此,如果我们正在查看“Photoshop”,则上述功能仅显示:
Photoshop-ps主题#1
-ps主题#2
它不显示“后效应”或儿童,ae主题1或ae主题2。
还可以使用var_dump($current);
.您将看到所有可用信息。例如,正如您将在上面看到的,我在函数中留下了一些代码$current_parent = $current->parent;
.
例如,您可以检查:
// check if current parent is not = 0
if ($current_parent != \'0\') {
// code if parent is NOT 0
// that always means that we are viewing a child
} else {
// code if parent is 0
// if parent is 0 we are sure we are on a top level
}
但是还有更多的信息/数据
$current
也许你可以用。我希望这有帮助。