WordPress 主題函式 get_the_category() 詳解使用
說明
獲取與查詢引數相匹配的類別物件
用法
- <?php get_the_category( $id ) ?>
引數
id
(整數) (可選) 文章編號
預設: $post->ID (當前文章的編號)
例子
顯示類別的圖片
- <?php
- foreach((get_the_category()) as $category) {
- echo '<img src="http://www.2zzt.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
- ?>
顯示第一個類別的名稱
- <?php
- $category = get_the_category();
- echo $category[0]->cat_name;
-
- <?php
- $category = get_the_category();
- if($category[0]){
- echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
- }
-
- <?php
- global $post;
- $categories = get_the_category($post->ID);
- $categories);
- ?>
返回物件的成員
cat_ID
類別編號,儲存在 term_id 欄位
cat_name
類別名稱,儲存在 name 欄位
category_nicename
別名,儲存在 slug 欄位
category_description
類別描述,儲存在 description 欄位
category_parent
父類別編號,沒有父類的為 0,儲存在 parent 欄位
category_count
類別使用的數量,儲存在 count 欄位