如何获得当前帖子的父类别

时间:2014-01-26 08:54:56

标签: wordpress parent

假设我正在为每个父母创建10个父类别和2个子类别。我的Wordpress帖子属于特定父类别的一个子类别

我如何才能获得父类别名称?我不想要子类别名称?什么Wordpress代码会这样做?

1 个答案:

答案 0 :(得分:0)

你可以试试这样的东西,找到帖子的顶级类别。 帖子也应与类别和子类别相关联。

//get all categories of current post
$categories = get_the_category($post->ID);

//get top level category of current post
$top_cat_obj = array();
foreach($categories as $cat) {

    if ($cat->parent == 0) {
        $top_cat_obj[] = $cat;  
    }

}           
$top_cat_name = $top_cat_obj[0]->name;
$top_cat_slug = $top_cat_obj[0]->slug;
相关问题