Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In this function, I need to exclude any categories from $categories which are children of "Uncategorized".

function get_cats(){
    $post_cats= array();
    $categories = get_the_category();
    foreach($categories as $cat){  
        array_push($post_cats, $cat->cat_ID);
    }
    return $post_cats;
}

Thanks in advance if you can help.

share|improve this question

3 Answers 3

I believe this can help (inside foreach):

if( $cat->cat_name != 'Uncategorized' ) {
    array_push( $post_cats, $cat->cat_ID );
}

Update:

You may want to use post_is_in_descendant_category function:

if( $cat->cat_name != 'Uncategorized'
    && !post_is_in_descendant_category(
           get_term_by( 'name', 'Uncategorized', 'category' ),
           $post
        )
) {
    array_push( $post_cats, $cat->cat_ID );
}

(not tested, sorry)

Nothe, that post_is_in_descendant_category should be defined in your functions.php, please check the link.

share|improve this answer
    
Thanks brownian, will this exclude the child cats of uncategorized too? Or do they have to be explicitly included by name as well? –  N2Mystic Nov 20 '11 at 18:49
    
@N2Mystic i've updated the answer –  brownian Nov 21 '11 at 7:39

Check if $cat->category_parent != 1 (assuming uncategorized has the default ID of 1). If your subcategories are deeper than one level you'll need to use get_ancestors to determine the top level category.

share|improve this answer

It should be like this,

function get_cats(){
$post_cats= array();
$categories = get_the_category();
  foreach($categories as $cat){
     if($cat->category_parent == 1 || $cat->cat_ID == 1)
     continue;
     array_push($post_cats, $cat->cat_ID);
  }
return $post_cats; }

Now the Un-categorized category and with this children filtered. You can remove this category from the admin if you need.

check this blog please.

http://computeraxe.com/how-to-get-rid-of-that-uncategorized-category-in-wordpress-blogs/

Thanks. I hope someone get this small thing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.