2016-11-09 69 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(); 
} 


參考: