$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display sub categories and their data of a taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display sub categories and their data of a taxonomy

matteradmin9PV0评论

I want to output the sub category details of a taxonomy

$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);

When i print the above out it returns the object. But theres no value difference between parent and sub categories. The first is a parent taxonomy and the second is a sub-category.

[1] => stdClass Object
    (
        [term_id] => 23
        [name] => Corporate teams
        [slug] => corporate-teams
        [term_group] => 0
        [term_taxonomy_id] => 23
        [taxonomy] => team_names
        [description] => Description of corporate team
        [parent] => 0
        [count] => 0
        [image_id] => 0
    )

[3] => stdClass Object
    (
        [term_id] => 25
        [name] => Team name 1
        [slug] => team-name-1
        [term_group] => 0
        [term_taxonomy_id] => 25
        [taxonomy] => team_names
        [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nec pellentesque sapien.
        [parent] => 22
        [count] => 1
        [image_id] => 90
    )

How would i query part of this object (likely as a foreach or as a WP_Query) so it will return the sub categories and their relavant values (title, image,etc)? Is the 'get_terms()' function the right way to go for this?

I want to output the sub category details of a taxonomy

$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);

When i print the above out it returns the object. But theres no value difference between parent and sub categories. The first is a parent taxonomy and the second is a sub-category.

[1] => stdClass Object
    (
        [term_id] => 23
        [name] => Corporate teams
        [slug] => corporate-teams
        [term_group] => 0
        [term_taxonomy_id] => 23
        [taxonomy] => team_names
        [description] => Description of corporate team
        [parent] => 0
        [count] => 0
        [image_id] => 0
    )

[3] => stdClass Object
    (
        [term_id] => 25
        [name] => Team name 1
        [slug] => team-name-1
        [term_group] => 0
        [term_taxonomy_id] => 25
        [taxonomy] => team_names
        [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nec pellentesque sapien.
        [parent] => 22
        [count] => 1
        [image_id] => 90
    )

How would i query part of this object (likely as a foreach or as a WP_Query) so it will return the sub categories and their relavant values (title, image,etc)? Is the 'get_terms()' function the right way to go for this?

Share Improve this question asked Jan 30, 2015 at 11:38 amoretonamoreton 1571 gold badge1 silver badge9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

You can use the parent argument in get_terms() to get direct children of a term. You can use the child_of argument to get all descendents of a term.

Something like this.

$parent_id = 32;
$args = array(
    'parent' => $parent_id
);
$terms = get_terms( $taxonomy, $args );   
print_r( $terms );

If you want to programmatically get parent terms and within the loop get sub-categories, you could do something like this.

$taxonomy = 'your_tax';
$args = array(
    'parent' => 0 // to get only parent terms
);
$terms = get_terms( $taxonomy, $args );

foreach( $terms as $term ) {
    $children = get_terms( $taxonomy, array(
        'parent' => $term->term_id;
    ) );

    print_r( $children );
}

http://codex.wordpress/Function_Reference/get_terms

Since wordpress 4.5.0, taxonomies should be passed through 'taxonomy' argument in $args array :

$parent_id = 32;
$args = array(
    'taxonomy' => $taxonomy,
    'parent' => $parent_id
);
$terms = get_terms( $args );   
print_r( $terms );

get_terms in official doc

<div class="categories-item">
  <?php 
  $wcatTerms = get_terms('service_cat', array('hide_empty' => 0, 'parent' =>0)); 

  foreach($wcatTerms as $wcatTerm) :

  ?>
    <button class="accordion"><?php echo $wcatTerm->name; ?></button>
    <div class="panel">
     <?php 
  $wcatTerms1 = get_terms('service_cat', array('hide_empty' => 0, 'parent' =>$wcatTerm->term_id)); 
  foreach($wcatTerms1 as $wcatTerm1) :
     ?>
      <a href="<?php echo get_term_link( $wcatTerm1->slug, $wcatTerm1->taxonomy ); ?>"><?php echo $wcatTerm1->name; ?></a>
      <?php endforeach ;  ?>
    </div>
   <?php endforeach ;  ?>


    <!-- <button class="accordion">Section 3</button>
    <div class="panel">

    </div> -->

</div>
Post a comment

comment list (0)

  1. No comments so far