获取分类法

2022-07-27

注册了自定义分类法,我们添加分类以后,需要在前台调用分类法下面的分类。可以使用下面的函数:

$terms = get_terms( array('taxonomy' => 'post_tag','hide_empty' => false,) );

其中,taxonomy是分类法的名称,hide_emypt设置为false,显示所有分类。

函数参数说明

未指定值的变量使用以下参数默认值(下文中有说明)。下面的列表中含有$args,将改写默认值。

orderby — 默认值为’name’,可以是名称,计数或空(使用term_id)

order — 默认值为ASC。有效值也包括DESC。

hide_empty — 默认值为true。不返回空$terms。

fields — 默认值为all。

slug — 任何含有slug的term都可以作为该变量的值。默认为空字符串。

hierarchical — 是否返回层级分类法。默认值为true。

name_like — 默认值为空字符串。

pad_counts — 默认值为FALSE。值为true时将计算包括$terms在内的所有子辈。

get — 默认值为空。可通过为’all’赋值来改写’hide_empty’和’child_of’。

child_of — 默认值为0。获取该term的所有后代。

parent — 默认值为0。获取该term的直系子辈(即上辈明确为该值的term)。

函数使用举例

<?php
    $terms = get_terms(array(
      'taxonomy' => 'keshi',
      'parent' =>1307,
      'hide_empty' => false,  //默认值为true。不返回空$terms。
    ));
?>
<div class="parts">
	<div class="parts_con" id="partscon">
		<div class="hd">
			<ul>
      <? if ($terms):
        foreach($terms as $term):?>
				  <li><?php echo $term->name; ?><i></i></li>
      <? endforeach;
        endif;
      ?>
			</ul>
		</div>
		<div class="bd" id="partsconbd">
  
    <? if ($terms):
        foreach($terms as $term):?>
          <div class="con">
            <ul>
            <? $term_children = get_term_children($term->term_id,'keshi');
            if ( count($term_children) > 0 ): ?>
                <?
                  foreach($term_children as $term_child_id):
                    $term_child = get_term_by('id',$term_child_id,'keshi');
                ?>
                  <li><a href="<? echo get_term_link($term_child) ?>"><? echo $term_child->name ?><i></i></a></li>
                <? endforeach; ?>
            </ul>
          </div>
        <? 
          endif;
          if ( count( $term_children ) === 0 ) {
            echo 'The term has no children to display.';
          }  
          endforeach;
        endif;
      ?>
		</div>
	</div>
</div>
相关内容
最新

坚持的力量

wordpress建站,视频剪辑拍摄,动画制作