You can use get_term_children() to check if a specific term has any children, and if yes, display the children; otherwise, display the parent”s children.
So just change the if ($term->parent == 0) { to:
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
Or here’s the full code I used:
$term = get_queried_object();
$children = get_term_children( $term->term_id, $term->taxonomy );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
wp_list_categories( 'taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=" . $term->term_id );
} else {
wp_list_categories( "taxonomy=' . $term->taxonomy . '&depth=1&show_count=0&title_li=&child_of=" . $term->parent );
}
Related Posts:
- What does the token %1$s in WordPress represent [closed]
- exclude multiple terms using get_terms() function
- Getting key value of WP_Term object in wordpress
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Archive listing of posts by publish year (multiple years)
- Add custom field to attachments in a specific category
- How to Remove Parents Category Permalink from Posts
- Display all posts in current category
- Looking to exclude blog posts from category Previous/Next buttons
- How to display related posts from parent category
- How can I set custom order use this function
- Put a link to a category round a hard coded A HREF
- Check if Product is in a Specific Category in Functions.php
- How to exclude specific category from the get_the_category(); array
- Add HTML Attributes To Anchor Tags In `wp_list_categories()` Function
- Category Thumbnail Display – How to display ONLY the main Category
- Where do posts get the sidebar from?
- combine Code 1 with Code 2
- get post based on category chosen in drop down – The ajax method
- Creating a related post section based on similar categories
- Swapping wp_dropdown_categories function with wp_category_checklist
- Proper syntax or method for keeping url in modified isotope / category links
- Issue adding sub category programmatically
- Grab posts by multiple categories
- selected option if current category is the value
- get_template_part based upon post’s category
- how to save selected option in variable for rest api category filter
- Recent Posts Not Showing Only On A Specific Category Page [closed]
- Alert Bar section within WP loop is displaying even though there are no posts
- Different post styles depending on category
- Display category name only once inside loop
- Display most popular posts of category
- get taxonomy thumbnail and use it as a variable in code
- Get latest post from all categories except one
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- Use get() method to grab all categories and output inside another method
- Only show size attributes in product box woocommerce when available
- Custom post type category not displaying category ID
- How to separate categories with commas?
- Show all tags within a category?
- Displaying list of posts in category page
- Echoing a CSS class based on category of post in a list
- Alphabetically Ordered Category in WordPress
- Loading Posts & Category with Ajax
- How to get current category
- Show parent categories of the current category
- is user member of a group, show them categories [closed]
- Showing Subcategory Name/Link Instead of Parent Category
- How to check if a category has a parent and child categories?
- WordPress custom taxonomy
- Finding and removing duplicates within WP Arrays
- Using multiple variables to assign categories to an array
- Compare current post Category in select menu
- Get category id when SEO URL is turned on
- Display latest post from WordPress Featured Category that is also in X,Y,or Z categories
- Get the list of post categories
- Display Sibling WooCommerce Product Categories on Archive Page
- Pagination at category doesnt work with same name of page
- Woocommerce. Get a list of products in the current category on the archive page and product page
- How to show categories and date on posts
- display the months of a specific category
- List sub categories and corresponding posts of a parent category
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- Allow two posts (from different categories) to have the same slug
- Dropdown category field inside repeatable metabox
- Child-Theme Category View with modified permalinks (%category% removed)
- How to move a post from one category to another
- How to enable HTML tags in category description without breaking the category page
- Trying to show the category of a post in the post display
- Create category after theme setup and modify the default one
- How can i show specific Category List?
- All categories options or All categories not Populating
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- Displaying 3 Category Posts differently
- Code works on page-example.php by not category-example.php
- get_category display only 1
- How to list categories by page id in wordpress
- WordPress using get_term to retreive slug not working as expected
- How to add style in functions.php depending on conditions?
- Showing the project type in HREF
- How do I display Category titles on a WP Page?
- Front end post submissions do not get submitted in the category
- problem with category menu, it doesn’t want to show link
- Make an array listing custom taxonomy
- Get category id for a custom category and display it in a class
- Saving category as insert post
- Trying to exclude posts from a category on the home page
- Trying to see if page is category or single and displaying title with appropriate heading tag
- wp_get_archive for category returning different URLs on different but similar sites
- Showing Subcategory’s Name and Link Instead of Parent Category’s
- get_category only returning details for ‘uncategorized’
- Hide empty categories from get_categories
- on category page get name of category as a string
- WordPress categories not working throughout site
- Remove /category/ from category (archive) page URLs (without using a plugin)
- How to create categories out of a list of words?
- Get Products within Current Product Category
- How to insert the first letter in uppercase [closed]
- Is there a way of displaying related posts from lowest hyrachical order (lowest child category to parent category)
- Can’t assign menu parent id or menu item breaks