获取分类数据

根据文章ID获取文章分类数据:get_the_category()

根据分类别名获取分类数据:get_category_by_slug()

<?php $cat = get_the_category($post_id);//返回数组,参数默认为当前文章ID ?>
<?php $cat = get_category_by_slug($slug);//返回数组?>

根据分类ID或分类对象获取分类数据:get_category()

<?php $cat = get_category( $category,$output,$filter);?>
<?php
$cat = get_category_by_slug($slug);//返回数组?>
<?php
$cat_id = $cat->term_id;//获取分类ID
$cat_count = $cat->count;//获取分类文章数
$cat_description = $cat->description;//获取分类描述
$cat_slug = $cat->slug;//获取分类别名
$cat_parent = $cat->parent;//获取分类父级引用
 ?>

获取有文章的分类列表:get_the_category_list()

<?php echo get_the_category_list();//返回字符串,为ul列表 ?>

输出结果

<ul class="post-categories">
    <li>
        <a href="http://seorss.net/category/wordpress" title="View all posts in Business" rel="category tag">wordpress</a>
    </li>
</ul>
<?php get_the_category_list($separator,$parents,$post_id);
//$separator:分类之间的分割符
//$post_id:根据文章ID获取分类列表
?>

根据分类ID获取分类名称:get_cat_name()

<?php $cat_name = get_cat_name( $cat_id );?>

根据分类ID获取分类链接(URL):get_category_link()

<?php $cat_url = get_category_link($category_id);//返回URL字符串 ?>

根据分类名称获取分类ID:get_cat_ID()

<?php $cat_id = get_cat_ID($cat_name);//返回分类ID ?>

标签: