This is a bit broad to give you the full code, so ‘I’ll give you an outline. The idea is to loop through all categories and count how many parents each category has using get_term_parents_list
(please look carefully what this function returns). This will tell you how deep the level of this category is. Then store the result in a multidimensional array. Like this:
$cat-levels = array(); // multidimensional array in which to store results
$categories = get_categories(); // retrieve an array of all categories (as objects)
foreach ($categories as $category) {
$parents = get_term_parents_list ($category->ID, 'category');
$count = // write code to extract amount of parents returned
$cat-levels[$count][] = $category;
}
Now $cat-levels[0]
will contain all categories without parents, $cat-level[1]
will have the categories one level deep. And so on.
Related Posts:
- WooCommerce – Display product child categories from specific parent
- Show subChild categories
- Getting child category id (help with improving my code)
- get_category_children/ the new get_term_children not work
- How would I get a taxonomy/category list inside a Gutenberg block?
- Gutenberg editor add a custom category as wrapper for custom blocks
- Rewrite rule page url with category
- WP_Query not looking at child category
- Deleting default category in wordpress
- Redirecting specific category to subdomain
- How to make all posts in a category sticky?
- WordPress: category page not for post’s
- Advanced Custom Fields: Using category as a rule, before publishing new post?
- Save metabox checkboxes values to custom content type
- WordPress check if user is logged in from non wordpress page
- Automatically assign posts by author to a specific category
- How to disable access to category pages
- How do I remove the tags from wp_list_categories?
- Show pages and articles in category search result
- How to group the loop posts in a particular by author?
- How to display get_the_category of post showing only CHILD of category “X”
- Get ID of current taxonomy in register_rest_field
- Custom page for WooCommerce’s /product-category/
- Add Class While on Current Post; wp_list_categories
- get all sub categories without specify any category
- How to use wp_category_checklist()?
- Get the name an the description of a link category
- Display child categories in WooCommerce
- WooCommerce duplicate sub categories and product slugs
- Specifying multiple categories in URL (permalink)?
- Check child/parent categories if exists
- How to run select query of post with category and tags for API?
- WordPress Genesis not excluding category from blog
- How to show only one category in breadcrumb navigation
- Trying to only display 1 category using get_posts
- How to display singular form of category name?
- Select posts by name and category per REST API
- Categories for each brands
- Custom User-Created RSS Feed
- How to create a page which lists all categories with its ratings [closed]
- Permalinks: display just one kind of wp category in the posts url
- Get value of custom field in category admin
- How do I make the category sidebar widget (stock) display the total # of posts including those in subcategories?
- Woo Framework: woo_cat_featured not populating
- Show category name, and last 5 post titles below category name?
- get_post_meta not working on category.php
- How can I improve this taxonomy treating approach?
- How to show more than 5 posts of the category?
- Dropdown Categories used in form shows no results when all is selected
- Get URL Category Name By ID
- Odd results in get_the_terms vs wp_get_post_terms
- Redirect Changed Permalink on wordpress
- Grandchild Term Things Grandfather Term is It’s Parent
- get full category structure by post id
- Home Page Template – Specific Category
- Same menu for different taxonomies to reach different content
- Using categories with pages
- Show next post in same category but start from the latest
- List all posts of certain category which share some tags
- Search form options group categories
- Natural sort / ordering wp_dropdown_categories
- Make wp_dropdown_categories into a variable that persists until changed?
- show category in woocommerce
- Can I define multiple paginations on a single page?
- Echo Category Nicename
- Category sticky latest
- How can I exclude a category from the main loop with the category name & not ID?
- Explode() expects a string
- How to add meta description, keywords, custom title to a category template
- how to remove alphabetical order of category in WordPress?
- How can i add service category here? [duplicate]
- How to exit out of delete_categories and stop action?
- Meta Query | Compare category name and meta value
- How to filter tags by category?
- Programmatically Create Category and sub Category and sub sub Category
- How Can I Display the Category Description in a Theme with no Category.php or Archive.php?
- WooCommerce – Get products from category right before deleting category
- omit categories from main navigation menu
- How to test if the post is in a primary category
- How to get the term id inside the start lvl method of category walker?
- Most efficient way to display current post subcategories?
- How to relocate images
- Category next page link not working
- How to display the past events in one of the category?
- How to Query the Top 5 Posts of a specific category?
- How to add a post with new Taxonomy without assigning to default category?
- Page Type with Category Link
- Using wp_list_pages to create 2 lists of pages and include and exclude some of them depending on their category
- How to create default categories in new installs?
- If I change the taxonomy of a post will it get a new time stamp?
- Check if value is a custom taxonomy category name
- Tracking multiple tags or categories
- 2 loops in archive.php (one for each category)
- How to load jquery tag-it plugin into admin?
- not empty categories don’t show in menu [closed]
- How do I add and display a custom image field to a category? [duplicate]
- Category pagination not working in category.php please help me
- Print the associated categories of the current post starting with parent (with option to remove href)
- Add category image before post title on homepage
- How to remove the category from the url only for a specific category of articles on wordpress