admin管理员组文章数量:1025457
I am using the code off this page: /
However my Custom taxonomy has more depth for cities, state, country.
I need "if ($term->parent == 0)" to search for if term has child, then do this, else then do this. Right now, parent taxonomy displays it's parent children, but I need parent taxonomy display it's children taxonomies and children taxonomy displays parents children. In essence both parent and child taxonomies would display the same cities if you understand what I mean on their respective pages.
So you goto Michigan page(parent), it displays Boston, Detroit, Grand Rapids etc. If you goto Boston page(child), it would still display Boston, Detroit, Grand Rapids etc. because there are no more children.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
I am using the code off this page: https://www.wpbeginner/wp-tutorials/how-to-display-child-taxonomy-on-parent-taxonomys-archive-page/
However my Custom taxonomy has more depth for cities, state, country.
I need "if ($term->parent == 0)" to search for if term has child, then do this, else then do this. Right now, parent taxonomy displays it's parent children, but I need parent taxonomy display it's children taxonomies and children taxonomy displays parents children. In essence both parent and child taxonomies would display the same cities if you understand what I mean on their respective pages.
So you goto Michigan page(parent), it displays Boston, Detroit, Grand Rapids etc. If you goto Boston page(child), it would still display Boston, Detroit, Grand Rapids etc. because there are no more children.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
Share
Improve this question
asked Apr 4, 2019 at 0:05
Joe LandryJoe Landry
277 bronze badges
1 Answer
Reset to default 1You can use get_term_children()
to check if a specific term has any children, and if yes, display the children; otherwise, display the parent's children.
So just change the if ($term->parent == 0) {
to:
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
Or here's the full code I used:
$term = get_queried_object();
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->term_id );
} else {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->parent );
}
I am using the code off this page: /
However my Custom taxonomy has more depth for cities, state, country.
I need "if ($term->parent == 0)" to search for if term has child, then do this, else then do this. Right now, parent taxonomy displays it's parent children, but I need parent taxonomy display it's children taxonomies and children taxonomy displays parents children. In essence both parent and child taxonomies would display the same cities if you understand what I mean on their respective pages.
So you goto Michigan page(parent), it displays Boston, Detroit, Grand Rapids etc. If you goto Boston page(child), it would still display Boston, Detroit, Grand Rapids etc. because there are no more children.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
I am using the code off this page: https://www.wpbeginner/wp-tutorials/how-to-display-child-taxonomy-on-parent-taxonomys-archive-page/
However my Custom taxonomy has more depth for cities, state, country.
I need "if ($term->parent == 0)" to search for if term has child, then do this, else then do this. Right now, parent taxonomy displays it's parent children, but I need parent taxonomy display it's children taxonomies and children taxonomy displays parents children. In essence both parent and child taxonomies would display the same cities if you understand what I mean on their respective pages.
So you goto Michigan page(parent), it displays Boston, Detroit, Grand Rapids etc. If you goto Boston page(child), it would still display Boston, Detroit, Grand Rapids etc. because there are no more children.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
Share
Improve this question
asked Apr 4, 2019 at 0:05
Joe LandryJoe Landry
277 bronze badges
1 Answer
Reset to default 1You can use get_term_children()
to check if a specific term has any children, and if yes, display the children; otherwise, display the parent's children.
So just change the if ($term->parent == 0) {
to:
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
Or here's the full code I used:
$term = get_queried_object();
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->term_id );
} else {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=' . $term->parent );
}
本文标签: phpArgument for if termgt have child
版权声明:本文标题:php - Argument for if term-> have child? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745630122a2160108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论