最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - get_term_children specific no id

programmeradmin3浏览0评论

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 '<li><a 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 '<li><a 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

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 '<li><a 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 '<li><a 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

Share Improve this question edited Apr 21, 2020 at 11:01 user13320018 asked Apr 21, 2020 at 10:12 user13320018user13320018 32 bronze badges 4
  • In what context? The taxonomy archive? Also, please note that there's no such thing as a "sub taxonomy" or "macro". As suggested by the function names, they are called "terms", and terms belong to taxonomies. You can use get_queried_object_id() as $term_id if the queried object is the term whose children you want to find. – Jacob Peattie Commented Apr 21, 2020 at 10:17
  • @JacobPeattie clear 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 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 – user13320018 Commented Apr 21, 2020 at 10:31
  • You should include context like that in the question. The question as written does not provide the information that would be necessary to give you to correct answer. – Jacob Peattie Commented Apr 21, 2020 at 10:36
  • @JacobPeattie are you able to find the solution? – user13320018 Commented Apr 21, 2020 at 10:37
Add a comment  | 

1 Answer 1

Reset to default 0

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,
    ]
);
发布评论

评论列表(0)

  1. 暂无评论