Условие на наличие дочек в таксономии

Здравствуйте, уважаемые гуру вордпресс.
Есть проблемка, с которой борюсь. Подскажите пожалуйста

Суть в следующем. В архивной странице таксономии (в данном случае taxonomy-typeuslugi.php пробую реализовать такую штуку
Если мы находимся на странице рубрики таксономии, и в этой рубрики есть дочерние подрубрики – выводиться список подрубрик в виде таблиц.
Если же мы находимся на странице рубрики (подрубрики), и там дальше нет дочерних подрубрик – выводяться записи (плитками). Это сделал.
Но есть еще один момент. Если мы находимся на странице рубрики, где есть подрурики – выводиться все подрубрики плитками. И если в одной из подрубрик есть еще дочки, тогда список выводяться ее дочки через wp_list_categories.

Если же нет подрубрик – то просто кнопка "Подробнее"

Вот именно эта штука никак не получаеться. Подскажите, как лучше сделать.
Ниже привожу все код.

 
 <? $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id!="" && sizeof($children)>0)) { ?>

<!--has parent and child-->
    
<?php
  $terms = get_terms('typeuslugi', array ('parent'=>$term->term_id,  'orderby'=>'id', 'hide_empty' => 0, 'hierarchical' => 0));
  $count = count($terms);
  foreach ($terms as $term) { 
   $custom_field = get_field('catimg', 'typeuslugi_' . $term->term_id  );
  $price = get_field('price', 'typeuslugi_' . $term->term_id);
?>
 
 <div class="subfundament">
                    <a href="/typeuslugi/<? echo $term->slug; ?>" title="<? echo $term->name; ?>"><img src="<? echo $custom_field['sizes']['term']; ?>"  /></a>
                    <h3><? echo $term->name; ?></h3>
                    <p  class="descterm"><?php echo term_description( $term->term_id, 'typeuslugi' ) ?></p>
                    <? if($price !== '') { ?><p class="price"><? echo $price ?><span> руб</span></p><? } ?>
                    <a class="orangemore" href="/typeuslugi/<? echo $term->slug; ?>">подробнее</a>
                </div>
                
 
 <? } ?>



<? }elseif(($parent->term_id=="") && (sizeof($children)>0)) { ?>

<!--no parent, has child-->

<?php
  $terms = get_terms('typeuslugi', array ('parent'=>$term->term_id,  'orderby'=>'id', 'hide_empty' => 0, 'hierarchical' => 0));
  $count = count($terms); 
  foreach ($terms as $term) {  
  $custom_field = get_field('catimg', 'typeuslugi_' . $term->term_id  );
  $price = get_field('price', 'typeuslugi_' . $term->term_id);
?>
 
 <div class="subfundament">
                    <a href="/typeuslugi/<? echo $term->slug; ?>" title="<? echo $term->name; ?>"><img src="<? echo $custom_field['sizes']['term']; ?>"  /></a>
                    <h3><? echo $term->name; ?></h3>
                    
                    <div class="descterm"><?php echo term_description( $term->term_id, 'typeuslugi' ) ?></div>
                    <? if($price !== '') { ?><p class="price"><? echo $price ?><span> руб</span></p><? } ?>
                    
<?php
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array(
'child_of'    => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) {  ?>
<ul id="nav">
            <li><a href="#">Категории</a>
                <ul><? wp_list_categories('taxonomy=typeuslugi&show_count=0&hide_empty=0&title_li=&child_of='. $term->term_id); ?></ul>
            </li>
        </ul>        
<? } else { ?>
Подробнее
<? } ?>
    
    
                    
                </div>
                
 
 <? } ?>

<? } else { ?>

<!--has parent, no child-->
    <p>Тут будут записи</p>

        <? } ?>
Anonymous
Отправить
Ответ на: