1
votes

I'm having 2 level ( one sub-category only. No sub-sub category ) Category in WordPress. I need to get all sub categories without parent category. I didn't 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 with URL for that. I want to give for each subcategories.

Help me.

2

2 Answers

0
votes

you can try this one

<?php 

foreach(get_categories() as $cat) { 
   // echo '<li><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a>';
    $sub_cats = get_categories('parent='.$cat->term_id.'&hide_empty=0');
    if($sub_cats) {

        foreach($sub_cats as $sub_cat) {

        }


    }
}


?>

hope this will work for you

0
votes

this code may help to get all sub category with out specifying categoy

<?php global $wpdb;$prefix=$wpdb->prefix;

$subcateogyr_list=$wpdb->get_results("Select * from ".$prefix."term_taxonomy WHERE parent!='0'");

foreach($subcateogyr_list as $subcat){

    $subcat_name=$wpdb->get_var("select name from ".$prefix."wp_terms where term_taxonomy_id='$subcat['term_id']'");


}
?>