Chinaunix首页 | 论坛 | 博客
  • 博客访问: 835681
  • 博文数量: 372
  • 博客积分: 10063
  • 博客等级: 中将
  • 技术积分: 4220
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 11:36
文章分类

全部博文(372)

文章存档

2012年(372)

分类: 虚拟化

2012-03-20 20:53:38

Php代码 复制代码 收藏代码
  1. $categories = get_the_category();
  2. $topcategory= '';
  3. foreach($categories as $childcat) {
  4. $parentcat = $childcat->category_parent;
  5. if($parentcat>0){
  6. $topcategory = get_cat_name($parentcat);
  7. continue;
  8. }
  9. }
  10. $topcategory = (strlen($topcategory)>0)? $topcategory : $categories[0]->cat_name;
  11. ?>
category_parent; if($parentcat>0){ $topcategory = get_cat_name($parentcat); continue; } } $topcategory = (strlen($topcategory)>0)? $topcategory : $categories[0]->cat_name; ?>

有以上代码

我们可以轻松找到 某个子分类的顶级分类

在wordpress写文章的时候 我们只要选择当前的子分类即可

不需要把父级分类甚至顶级分类都勾选上

阅读(426) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~