CodyMan
CodyMan

Reputation: 153

How to display sub categories in wordpress

I'm trying to display Sub categories on main category page.

For example i have a category named as : live tv

And it is consist of some sub categories like : sports tv, cartoon tv, entertainment.

I want to display these sub categories with images inside content area of main category.

I was able to assign images to categories using plugin.

I have tried this code in category.php

<?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&show_count=0
    &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
    "&echo=0"); else
    $this_category = wp_list_categories('orderby=id&depth=1&show_count=0
    &title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
    "&echo=0");
    if ($this_category) { ?> 

<ul>
<?php echo $this_category; ?>

</ul>

<?php } ?>

But that code isn't showing anything on my category page.

Upvotes: 0

Views: 127

Answers (1)

Samir Karmacharya
Samir Karmacharya

Reputation: 890

you can also get sub category as:

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

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

foreach($subcateogyr_list as $subcat

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

}
?>

Upvotes: 1

Related Questions