检查父类别是否具有父类别?

时间:2013-09-30 08:39:31

标签: php wordpress

我检查过类别'categoryone'是否有父类别,Yes categoryone是否有一个名为categorydad的父类别,知道我想检查categorydad是否有父类...

$tid = term_exists('categoryone', 'category', 0);

    $term_ids = array();

    if ( $tid !== 0 && $tid !== null )
    {
        $term_ids[] = $tid['term_id'];
    }
    else
    {
        // Finns inte!
        $insert_term_id = wp_insert_term( 'categoryone', 'category' );

        // var_dump( $insert_term_id );

        if ( ! is_wp_error )
            $term_ids[] = $insert_term_id;

    }
    wp_set_post_categories( $insert_id, $term_ids );

1 个答案:

答案 0 :(得分:1)

function category_has_parent($catid){
    $category = get_category($catid);
    if ($category->category_parent > 0){
        return true;
    }
    return false;
}

if (category_has_parent('22')){
    //true there is a parent category
}else{
   //false this category has no parent
}

要检查相反的方式(如果某个类别有子项),您可以使用get_categories

$children = get_categories(array('child_of' => id,'hide_empty' => 0));
if (count($children) > 1){
    //has childern
}else{
    //no children
}

或者检查一下:http://alex.leonard.ie/2011/04/20/wordpress-get-id-of-top-level-parent-category/