I think you can just use get_terms()
for this. This is just some bare minimum code, I guess you also want to add permalinks, term description or other things.
However, I just tested this code (now tested with taxonomy “course”) and it works.
I try my best to comment the code as we go along:
// 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
Also try using print_r($top_level_term);
for example in the first foreach loop. With that, you can see all the info that comes with one single term.
I would say you should use this code in the normal taxonomy archive template and don’t use the file taxonomy-course-photoshop.php
. Because then you would need a separate template file for every single term, and that’s not practicable.
Edit:
Also, make sure you have some posts assigned to the single terms and sub-terms. Otherwise, the terms will not display at all!
Update:
Yes, you can get the current taxonomy and term data automatically.
If you are working inside taxonomy-course.php
than you can try this:
//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
So if we are viewing “Photoshop”, the above function shows ONLY:
Photoshop
– ps-thematic#1
– ps-thematic#2
It does NOT show “After Effects” or the children, ae-thematic#1 or ae-thematic#2.
Also take a look using var_dump($current);
.
You will see all available information. As you will see above, for example, I left some code in the function $current_parent = $current->parent;
.
For example you could check:
// 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
}
But there are some more infos/data in $current
that you can maybe use.
I Hope this helps.