get_term_children specific no id

hi everyone from the codex I found what I needed that is to show the sub-taxonomies by specifying the id of the sub-taxonomy and the macro taxonomy

get_term_children( int $term_id, string $taxonomy )

with the example shown

?php
$term_id = 10;
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo 'ul';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo 'lia href="' . get_term_link( $child, $taxonomy_name ) . '"' . $term-name . '/a/li';
}
echo '/ul';
? 

ok and this works perfectly, i was wondering if there was another possibility instead of specifying the id of the sub-taxonomy which is not exactly the best it is possible specifying by writing the name, how could it be done?

to have a typical result

?php
$term_id = 'telephone';
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo 'ul';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo 'lia href="' . get_term_link( $child, $taxonomy_name ) . '"' . $term-name . '/a/li';
}
echo '/ul';
? 

I wrote macro and micro only for understanding of question, however the context is "archive" taxonomy or the taxonomy-xxx.php, the problem is that I am in this situation(example) category-> training-> basketball, according to the archive page taxonomy basketball, I would like to let all the training children out and I would not do it with the id

Topic get-children taxonomy custom-taxonomy Wordpress

Category Web


Based on your comment:

the problem is that I am in this situation category-> training-> basketball, according to the archive page taxonomy basketball, I would like to let all the training children out and I would not do it with the id

What you're actually trying to do is get all terms that are 'siblings' of the current term i.e. terms with the same parent.

You can do this by using the current term's parent ID combined with get_terms(), like this:

$current_term = get_queried_object();
$siblings     = get_terms(
    [
        'taxonomy' => $current_term->taxonomy,
        'parent'   => $current_term->parent,
    ]
);

echo '<ul>';

foreach ( $siblings as $sibling ) {
    echo '<li><a href="' . get_term_link( $sibling ) . '">' . $sibling->name . '</a></li>';
}

echo '</ul>';

Note that by using get_terms(), rather than get_term_children(), I avoid the need to use get_term_by().

You can simplify this code even further by using wp_list_categories() to output the HTML for a list:

$current_term = get_queried_object();

wp_list_categories(
    [
        'taxonomy' => $current_term->taxonomy,
        'parent'   => $current_term->parent,
    ]
);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.