正务库

wordpress主题函数 wordpress get_the_category() 详解

时间:2年前   游览量:406    标签: nbsp    category    字段    存储    gt    39    quot   

wordpress主题函数 get_the_category() 详解使用

说明

获取与查询参数相匹配的类别对象

用法

  1. <?php get_the_category( $id ) ?>

参数

id

(整数) (可选) 文章编号

默认: $post->ID (当前文章的编号)

例子

显示类别的图片

  1. <?php

  2. foreach((get_the_category()) as $category) {

  3.     echo '<img src="http://www.2zzt.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';

  4. }

  5. ?>

显示第一个类别的名称

  1. <?php

  2. $category = get_the_category();

  3. echo $category[0]->cat_name;

  4. ?>

显示第一个类别的连接

  1. <?php

  2. $category = get_the_category();

  3. if($category[0]){

  4. echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';

  5. }

  6. ?>

获取指定文章编号的类别信息

  1. <?php

  2. global $post;

  3. $categories = get_the_category($post->ID);

  4. var_dump($categories);

  5. ?>

返回对象的成员

cat_ID

类别编号,存储在term_id字段

cat_name

类别名称,存储在name字段

category_nicename

别名,存储在slug字段

category_description

类别描述,存储在description字段

category_parent

父类别编号,没有父类的为0,存储在parent字段

category_count

类别使用的数量,存储在count字段