Im using the following code to get an array of children taxonomies and write them out with links in an unordered list.
<?php
$termID = 10;
$taxonomyName = "products";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
What I’m trying to achieve is to get the actual term (category) id so I can replace it on $termID and don’t have to hardcode the id of the term.
Any help would be kindly appreciated!
Thanks!
Here is a function I use to list subterms:
Use it like
wp_list_categories()
.Avoid
get_term_by()
. It is very expensive and not necessary.To get the Current term you can use
get_query_var( 'term' );
and to get the current taxonomy you can useget_query_var( 'taxonomy' )
so you can do something like this:Or you can use : term_exists( $term, $taxonomy, $parent )
See WordPress Codex
To get the current term ID, use:
get_query_var
cannot be used in thise case, sinceterm_id
is not in the list of vars available publicly.