简体   繁体   中英

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.

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']'");

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM