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.

I have 2 levels of categories ( one sub-category only. No sub-sub category ). I need to get all sub categories without parent category. (I don't want to specify any parent category.)

Example:

p_cat1
   s_cat1
   s_cat2
p_cat4
p_cat3
   s_cat4
   s_cat5
   s_cat7

from here, I need

s_cat1
s_cat2
s_cat4
s_cat5
s_cat7

And, I need there to be a URL for each of those sub-categories. I want to give <a> for each subcategory.

How do I do that?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

You could use get_categories().

Returns an array of category objects matching the query parameters.

Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.

// Fetch parent categories
$parent_categories = get_categories( 'parent=0' );

foreach ( $parent_categories as $parent_category ) {
  // Fetch child categories
  $args = array(
    'parent' => $parent_category->term_id
  );

  $categories = get_categories( $args );

  foreach ( $categories as $category ) {
    printf( '<div>%s</div>', $category->name );
  }
}

This is a very simple example of the code without extra parameters like hide_empty, type etc.

share|improve this answer
add comment

The get_categories function of Wordpress returns all the categories, you can loop over the list of categories returned by get_categories and check for the parent property, if parent is '0' then its the parent category other wise it is the sub category.

function get_sub_categories() {
    $cats = get_categories();
    $subcats = array();
    foreach( $cats as $cat ) {
        if ($cat->parent != '0') {
            $subcats[] = $cat;
        }
    }
    return $subcats;
}

I don't know how you want with URL but you can try the following function which will only return Sub categories name hyper linked to their category URLs

function get_sub_categories() {
    $cats = get_categories();
    $subcats = array();
    foreach( $cats as $cat ) {
        if ($cat->parent != '0') {
            $subcats[] = '<a href="' . get_category_link( $cat->term_id ) .'">' . $cat->name . '</a>';
        }
    }
    return $subcats;
}
share|improve this answer
    
thanks, I need with url. I want to give <a> for each subcategories. –  KarSho Mar 20 '13 at 10:13
    
This would work, but it would also get any Category that is not top level, not just Level 2. I think that is what the OP specified (although the question isn't 100% clear - he may have mentioned Level 2 as that is all he has set up!). –  David Gard Mar 20 '13 at 10:16
    
David the above function will get any category that is NOT top level which means all sub categories. –  Hameedullah Khan Mar 20 '13 at 10:26
add comment

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.