`

wordpress技巧之 找到分类的顶级分类

阅读更多
<?php				
				   $categories = get_the_category();
				   $topcategory= '';
				   foreach($categories as $childcat) {
				       $parentcat = $childcat->category_parent;
				       if($parentcat>0){
					   $topcategory = get_cat_name($parentcat);
					   continue;
					}
				   }
				   $topcategory = (strlen($topcategory)>0)? $topcategory :  $categories[0]->cat_name;
 ?>

 有以上代码

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

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

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

 

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics