2016-11-09 66 views
0

我试图获得单个帖子的第二级类别。从WP获取类别ID

一个帖子是在这个层次体育>足球>英超联赛

我想找到ID为体育我怎么能做到这一点。

谢谢。

回答

0

您可以通过此代码为特定职位

$cat = get_the_category($post_id); 

然后让所有的第一级类别的这一个,你可以得到孩子类别

$child_categories=get_categories(
    array('parent' => $cat->cat_ID) 
); 
0

试试这个,这会为“第n个工作'等级

$category = get_the_category(); 
$parent = get_ancestors($category[0]->term_id,'category'); 
if (empty($parent)) { 
    $parent[] = array($category[0]->term_id); 
} 
$parent = array_pop($parent); 
$parent = get_category($parent); 
if (!is_wp_error($parent)) { 
    var_dump($parent); 
} else { 
    echo $parent->get_error_message(); 
} 


参考: