wordpress get_the_category()函数

技术文章 2014年9月28日 5.14K

get_the_category()

说明

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

用法

<?php get_the_category( $id ) ?>

参数

id

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

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

例子

显示类别的图片

<?php

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

echo ‘<img src=”http://example.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);

var_dump($categories);

?>

返回对象的成员

cat_ID

类别编号,存储在term_id字段

cat_name

类别名称,存储在name字段

category_nicename

别名,存储在slug字段

category_description

类别描述,存储在description字段

category_parent

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

category_count

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

源码位置

wp-includes/category-template.php


关注微信公众号『PHP学习网

第一时间了解最新网络动态
关注博主不迷路~

PHP学习网:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

您需要之后才可以评论
0点赞 0评论 收藏 QQ分享 微博分享

PHP学习网

PHP学习网