wordpress函数之get_terms

该函数用来查询某一 taxonomy 下的所有分类,即wp_term_taxonomy和wp_terms表的联合查询结果。

用法如下 :

<?php  
    $terms = get_terms([
	    /* wp_term_taxonomy 表的 taxonomy 字段值
		   代码层面就是 register_taxonomy 注册的分类方法 */			
        'taxonomy' => 'product_category',
        // 如果该分类下没有文章,是否显示 
        'hide_empty' => false,
        /*该分类的上级分类,即 wp_term_taxonomy 表的 parent 字段,
         如果查询一级分类,设置为 0 即可。
         如果查询子分类,这里只需指定父分类的term_id即可
         wp_term_taxonomy 表的 parent 字段,指的是 term_id 字段 */         
        'parent' => 0,
        /* 根据哪个字段进行排序,网上找的代码这里写id就能排序,不知道这个id是哪个表的id
	       估计应该是 term_id 字段 */
        'orderby'=> 'id',
        // 升序排序
        'order'  => 'ASC'
    ]);
    foreach($terms as $term):
?>

<a href="javascript:void(0)" class=""><?php echo $term->name;  ?></a>

<?php endforeach; ?>

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注