admin管理员组文章数量:1122846
I'm creating an age select menu in the admin, populated from a taxonomy of age
. The taxonomy is hierarchical as follows:
- 18-25 (parent, ID 183)
- 18 (child)
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26-30 (parent, ID 184)
- 26
- 27
- 28
- 29
- 30
I would like to only list the children (18, 19 etc) and not the parents (18-25, 26-30) etc. Currently I am using get_terms
with the parent
argument, but it doesn't accept more than 1 parent ID. Here's what I have so far, which shows the children from 18-25.
$ages = get_terms( 'age', array(
'hide_empty' => 0,
'parent' => '183',
));
Here's what I want it to do, but isn't supported. I have also tried it with an array but it doesn't work either.
$ages = get_terms( 'age', array(
'hide_empty' => 0,
'parent' => '183,184',
));
I see there is a get_term_children function but I'm unsure of how to use this either as it looks like it only accepts one value also. Eg: In this example it would build an unordered list but I could modify for select menu.
<?php
$termID = 183;
$taxonomyName = "age";
$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>';
?>
I'm creating an age select menu in the admin, populated from a taxonomy of age
. The taxonomy is hierarchical as follows:
- 18-25 (parent, ID 183)
- 18 (child)
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26-30 (parent, ID 184)
- 26
- 27
- 28
- 29
- 30
I would like to only list the children (18, 19 etc) and not the parents (18-25, 26-30) etc. Currently I am using get_terms
with the parent
argument, but it doesn't accept more than 1 parent ID. Here's what I have so far, which shows the children from 18-25.
$ages = get_terms( 'age', array(
'hide_empty' => 0,
'parent' => '183',
));
Here's what I want it to do, but isn't supported. I have also tried it with an array but it doesn't work either.
$ages = get_terms( 'age', array(
'hide_empty' => 0,
'parent' => '183,184',
));
I see there is a get_term_children function but I'm unsure of how to use this either as it looks like it only accepts one value also. Eg: In this example it would build an unordered list but I could modify for select menu.
<?php
$termID = 183;
$taxonomyName = "age";
$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>';
?>
Share
Improve this question
asked Jul 24, 2011 at 12:51
AndrewAndrew
1,1633 gold badges24 silver badges47 bronze badges
3
|
6 Answers
Reset to default 19This should work for you:
$taxonomyName = "age";
//This gets top layer terms only. This is done by setting parent to 0.
$parent_terms = get_terms(
$taxonomyName,
array(
'parent' => 0,
'orderby' => 'slug',
'hide_empty' => false
)
);
echo '<ul>';
foreach ( $parent_terms as $pterm ) {
//Get the Child terms
$terms = get_terms(
$taxonomyName,
array(
'parent' => $pterm->term_id,
'orderby' => 'slug',
'hide_empty' => false
)
);
foreach ( $terms as $term ) {
echo '<li><a href="'. get_term_link($term) . '">'. $term->name .'</a></li>';
}
}
echo '</ul>';
You could also do:
$terms = get_terms($taxonomyName);
foreach($terms as $term) {
if ($term->parent != 0) { // avoid parent categories
//your instructions here
}
}
I've noted that parent have "parent" field equal to 0, and a child have his parent id in it.
We can exclude the top level parents by filtering them out by using the terms_clauses
filter to alter the SQL query before it executes. This way we do not need to skip parents in the final foreach
loop as they are not in the returned array of terms, this saves us unnecessary work and coding
You can try the following:
add_filter( 'terms_clauses', function ( $pieces, $taxonomies, $args )
{
// Check if our custom arguments is set and set to 1, if not bail
if ( !isset( $args['wpse_exclude_top'] )
|| 1 !== $args['wpse_exclude_top']
)
return $pieces;
// Everything checks out, lets remove parents
$pieces['where'] .= ' AND tt.parent > 0';
return $pieces;
}, 10, 3 );
To exclude top level parents, we can now pass 'wpse_exclude_top' => 1
with our array of arguments. The new wpse_exclude_top
parameter is supported by the filter above
$terms = get_terms( 'category', ['wpse_exclude_top' => 1] );
if ( $terms
&& !is_wp_error( $terms )
) {
echo '<ul>';
foreach ($terms as $term) {
echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
}
Just a note, get_term_link()
do no not accept the term name, only, slug, ID or the complete term object. For performance, always always pass the term object to get_term_link()
if the term object is available (as in this case)
Why can't you just set the childless
argument to true?
$ages = get_terms( array(
'taxonomy' => 'age',
'childless' => true
)
);
$taxonomy = 'age';
foreach( get_terms( [ 'taxonomy' => $taxonomy, 'parent' => 0 ] ) as $parent )
{
foreach( get_terms[ 'taxonomy' => $taxonomy, 'parent' => $parent->term_id ] as $child )
{
// do stuff
print_r( $child );
}
}
If you display multiple parent's child, you can try this. Display mention term ids child term.
$termIds = array(367, 366, 365, 364, 363, 362);
$taxonomyName = "age";
$args = array(
'orderby' => 'term_id',
'order' => 'DESC',
'hide_empty' => false,
'childless' => false,
);
$terms = get_terms( $taxonomyName, $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
$inc = 1;
foreach ( $terms as $term ) {
if (in_array($term->parent, $termIds)) {
echo '<option value="'.$term->term_id.'"><font><font>'.$term->name.'</font></font></option>';
}
}
}
本文标签: How to only list the child terms of a taxonomy and not their parents
版权声明:本文标题:How to only list the child terms of a taxonomy and not their parents? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310349a1934345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_terms
twice and merge the two arrays of results? – t31os Commented Jul 24, 2011 at 15:21get_terms
call). Your question read as if you were wanting all children of 2 particular parent terms.. – t31os Commented Jul 26, 2011 at 16:03