如果您只有4个级别,并且每个第三级别术语都有子级,那么如果有子级,我们可以确定第四级别术语:
$term_id = get_queried_object()->term_id;
$taxonomy_name = \'product_range\';
$custom_terms = get_term_children( $term_id, $taxonomy_name );
foreach ( $custom_terms as $custom_term ) {
$term = get_term_by( \'id\', $custom_term, $taxonomy_name );
$terms_chilren = get_term_children( $term_id, $taxonomy_name );
wp_reset_query();
// if we can not get a term`s children, we can confirm the term is the fourth level term.
if ( ! empty( $terms_chilren ) && ! is_wp_error( $terms_chilren ) ) {
$args = [
\'post_type\' => \'product\',
\'posts_per_page\' => 1,
\'tax_query\' => [
[
\'taxonomy\' => \'product_range\',
\'field\' => \'slug\',
\'terms\' => $term->slug,
\'parent\' => 0,
],
],
];
}
}