Last active
August 29, 2015 14:01
-
-
Save aisin/8852ba1416288a8bf8dd to your computer and use it in GitHub Desktop.
WordPress single.php 获取当前文章的分类的父级 ID 和名称
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//获取ID | |
global $post; | |
$category = get_the_category($post->ID); | |
$cat_parent = get_category($category[0]->category_parent)->term_id; | |
if (!empty($cat_parent)) { | |
$cat_id = $cat_parent; //如果有父级分类,获取父级分类ID | |
} else { | |
$cat_id = $category[0]->cat_ID; //如果没有父级分类,获取当前分类ID | |
} | |
//获取名称 | |
global $post; | |
$category = get_the_category($post->ID); | |
$cat_parent = get_cat_name($category[0]->category_parent); | |
if (!empty($cat_parent)) { | |
$cat_name = $$cat_parent; //如果有父级分类,获取父级分类名称 | |
} else { | |
$cat_name = $$category[0]->cat_name; //如果没有父级分类,获取当前分类名称 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment