1

我检查了类别“categoryone”是否有父类别,是的,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 );
4

1 回答 1

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/

于 2013-09-30T08:45:10.880 回答