显示分类的子类别及其数据

时间:2015-01-30 作者:sledgeweight

我想输出分类法的子类别详细信息

$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);
当我打印出上面的内容时,它会返回对象。但父类别和子类别之间没有价值差异。第一个是父分类法,第二个是子类别。

[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
    )
如何查询此对象的一部分(可能作为foreach或WP\\u查询),以便返回子类别及其相关值(标题、图像等)?“get\\u terms()”函数是否正确?

3 个回复
最合适的回答,由SO网友:Duane 整理而成

您可以使用parent 中的参数get_terms() 获得一个学期的直接子女。您可以使用child_of 参数获取一个术语的所有后代。

像这样的。

$parent_id = 32;
$args = array(
    \'parent\' => $parent_id
);
$terms = get_terms( $taxonomy, $args );   
print_r( $terms );
如果希望以编程方式获取父术语,并在循环中获取子类别,可以执行以下操作。

$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.org/Function_Reference/get_terms

SO网友:ZalemCitizen

自wordpress 4.5.0以来,分类法应通过$args数组中的“taxonomy”参数传递:

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

get_terms in official doc

SO网友:Purnendu Sarkar
<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>
结束

相关推荐

显示分类的子类别及其数据 - 小码农CODE - 行之有效找到问题解决它

显示分类的子类别及其数据

时间:2015-01-30 作者:sledgeweight

我想输出分类法的子类别详细信息

$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);
当我打印出上面的内容时,它会返回对象。但父类别和子类别之间没有价值差异。第一个是父分类法,第二个是子类别。

[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
    )
如何查询此对象的一部分(可能作为foreach或WP\\u查询),以便返回子类别及其相关值(标题、图像等)?“get\\u terms()”函数是否正确?

3 个回复
最合适的回答,由SO网友:Duane 整理而成

您可以使用parent 中的参数get_terms() 获得一个学期的直接子女。您可以使用child_of 参数获取一个术语的所有后代。

像这样的。

$parent_id = 32;
$args = array(
    \'parent\' => $parent_id
);
$terms = get_terms( $taxonomy, $args );   
print_r( $terms );
如果希望以编程方式获取父术语,并在循环中获取子类别,可以执行以下操作。

$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.org/Function_Reference/get_terms

SO网友:ZalemCitizen

自wordpress 4.5.0以来,分类法应通过$args数组中的“taxonomy”参数传递:

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

get_terms in official doc

SO网友:Purnendu Sarkar
<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>

相关推荐