<?php
echo '<ul>';
foreach(get_the_category() 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) {
echo '<ul>';
foreach($sub_cats as $sub_cat) {
echo '<li><a href="'.get_category_link($sub_cat->term_id).'">'.$sub_cat->name.'</a></li>';
}
echo '</ul>';
echo '</li>';
}
}
echo '</ul>';
?>
edit:
for a list of sub categories in a category archive, try this code:
<?php
$cat = get_query_var('cat');
$cat_list = wp_list_categories('child_of=".$cat."&hide_empty=0&title_li=&echo=0');
if($cat_list) {
echo 'Sub-category List <ul>';
echo $cat_list;
echo '</ul>';
}
?>
Related Posts:
- How to display non-hierarchical taxonomy as terms with checkboxes?
- Different template for subcategories
- Categories’ hierarchy in URL
- How to only show posts on last child category
- Categories lose hierarchy order once assigned to post
- Add Product categories to WordPress menu without losing hierarchy
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- how to get the categories for a single post in a hierarchical way
- Category tree is flattened inside admin upon saving
- Categories vs. Page Site Organization
- How to enable category hierarchy for links?
- Combining post categories and link categories?
- How to categorize posts under Category 1 and Category 2 which are separate lists of categories, and display them as separate lists in widgets
- Give a page priority over a category that has the same name?
- Add child category to parent on post creation and save
- How do I make the category sidebar widget (stock) display the total # of posts including those in subcategories?
- How to get Next/previous_post_link to go through articles in top parent category
- Hierarchical permalink structure for posts showing categories and sub-categories
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- Display only subcategories of a category and not sub-subcategories
- How to load several hierarchical categories from functions.php
- Check is category parent with ids from the childs – get_term_children
- Posts not listing out sub category in hierarchy
- Show subChild categories
- Parent Category and Subcategory default text generated with category
- outputting taxonomy hierarchy
- Different template for subcategories
- Children categories not shown in dashboard
- Most efficient use of custom taxonomies, categories, and pages
- Display categories and their IDs
- List the category tree of all the product_cat categories
- Can I set a default featured image for a category?
- Hook when category is added to post
- get_categories for custom post type with a specific custom taxonomy attached
- Could not insert term into the database
- Activate Gutenberg in category-descriptions
- What’s the URL for a category archive?
- Weird behaviour when adding terms/term_taxonomies programmatically
- How can I get category ID by category name?
- How would I increase the Author Role to be able to add a new category?
- Exclude categories from Loop, queries, widgets, post navigation
- how to use a different domain/subdomain for authors/catagories on single site?
- Randomise results from a category page?
- WordPress 1 domain multiple blogs
- Category count inside the link
- Is there a way to have duplicate category slugs?
- Multiple level category drop-down
- Adding Custom User Profile data based upon Categories
- Only show category to certain user levels without plugin
- How to: 301 Redirect /category/ to /customname/
- One WordPress Install, Two Categories. Each Category Gets a Domain
- Name of last category level for a post
- How to add style to category link?
- WordPress Ordering Problem. How to fix ordering 1-10-100 issue?
- Custom Query to search through categories
- how to show posts of category random by session
- Order posts by category name
- How can I make wp_list_categories output li with category-slug as class, for its children?
- Get sticky post from category?
- Remove “Parent” Selection When Adding/Editing Categories?
- Remove word “Category” from WooCommerce product page [closed]
- Filtering search results
- WooCommerce change category url and product base
- How can we get Catergory / Term Creator
- Seperate WordPress catagories into sepeat list on seperate pages
- Redirect category to first available child post
- Show post only if match all categories
- Output link to category from WP_Query loop of woocommerce products
- Display name of the last child category
- How to get product count with respect to categories in WooComerce
- Password protect a specific category page/post
- If in category to be inside of a function
- How to exclude posts from a category when using this particular format
- Viewing category pages without the word ‘category’ in URL
- Different excerpt styles per category, but chronological
- Show only one category
- Pagination Not Working on Category.php page
- How to view WordPress’ default category IDs?
- Random taxonomy category list
- Display WordPress Parent Categories Only?
- Filter Home Page Posts in Buddypress [closed]
- Broken category pagination
- Additional featured post on first page
- Color different for the current category
- how to change template?
- Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Product Category WooCommerce
- Assign automatically and manually modify category
- Hard coded main navigation
- Displaying a full post instead of a slug per category?
- one category template for multiple categories
- Display posts separated by Category in Author’s page
- How can I define a custom archive for childs of a specific category?
- previous/next_post_link in the same sub-category?
- How do I toggle pagination on/off in search results and category listings via a link?
- Category shows only top level child categories
- The arrangement of categories always change while posting article
- get_categories() returns “Uncategorised”
- How to show WordPress parent and child category using a different template?
- How can I get categories IDs if multiple categories requested via URI?
- List categories with custom code