@veroni1337

Как вывести дочерние рубрики родителя get_terms wordpress?

Здравствуйте. Нужна помощь с выводом дочерних рубрик внутри родителя.
вот так:
<ul>
    <li>
        Родитель
        <ul>
              <li>дети</li>
               <li>дети</li>
        </ul>
    </li>
</ul>


пробую вывести вот так с помощью child_of, но ничего не выходит
<?php
                                $args = array( 'taxonomy' => 'projectscat', 'hide_empty' => true, 'parent' => 0 );
                                $product_categories = get_terms('projectscat', $args);
                                
                                foreach ($product_categories as $term) :
                                    echo '<li><a href="/your-category-base-url/' . $term->slug . '" title="' . sprintf(__('Category %s', 'zigcy-lite'), $term->name) . '">' . $term->name . ' ('.$term->count.')</a></li>';
                                    
                                ?>

                                    <li><a href="<?php $term->slug ?>">
                                        <ul>
                                        <?php $term->name . $term->count ?>
                                        <?php
                                            $termChild = $term->term_id;
                                            $termChildArray = get_terms([
                                                'taxanomy' => 'projectscat',
                                                'child_of' => $termChild,
                                                'hide_empty' => false,
                                            ]);
                                            
                                                foreach($termChildArray as $termChildArrayKey) :
                                        ?>
                                            <li>
                                                <?php echo $termChildArrayKey->name; ?>
                                            </li>
                                        <?php endforeach; ?>
                                        </ul>
                                    </a></li>

                                <?php
                                endforeach;
                                ?>
  • Вопрос задан
  • 128 просмотров
Решения вопроса 2
@IvanMogilev
Backend developer: Wordpress,Yii
Держите, скопировал с проекта своего, у меня тут ооп, думаю под функции переделаете
public static function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
      
        $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
    
        $terms = get_terms( $taxonomy, array( 'parent' => $parent ,'hide_empty'=>0) );

        
        $children = array();
       
        foreach ( $terms as $term ){
            
            $term->children = self::get_taxonomy_hierarchy( $taxonomy, $term->term_id );
          
            $children[ $term->term_id ] = $term;
        }
     
        return $children;
    }

    public function get_taxonomy_hierarchy_multiple( $taxonomies, $parent = 0 ) {
        if ( ! is_array( $taxonomies )  ) {
            $taxonomies = array( $taxonomies );
        }
        $results = array();
        foreach( $taxonomies as $taxonomy ){
            $terms = get_taxonomy_hierarchy( $taxonomy, $parent );
            if ( $terms ) {
                $results[ $taxonomy ] = $terms;
            }
        }
        return $results;
    }
Ответ написан
@veroni1337 Автор вопроса
Получилось вывести вот так:
<ul>
    <?php $hiterms = get_terms("название ваше таксономии", array("orderby" => "slug", "parent" => 0)); ?>
    <?php foreach($hiterms as $key => $hiterm) : ?>
        <li>
            <?php echo $hiterm->name; ?>
            <?php $loterms = get_terms("my_tax", array("orderby" => "slug", "parent" => $hiterm->term_id)); ?>
            <?php if($loterms) : ?>
                <ul>
                    <?php foreach($loterms as $key => $loterm) : ?>
                        <li><?php echo $loterm->name; ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы