Once you can’t know the order of categories returned by get_the_category, I suggest to loop through the categories and fill two arrays, one for parent and one for children.
After that if the parents array is empty showing the children, otherwise show the parents:
$categories = get_the_category();
if ( ! empty($categories) ) {
$parents = array();
$children = array();
foreach( get_the_category() as $cat ) {
if ( $cat->parent == 0 ) {
$parents[] = $cat;
} else {
$children[] = $cat;
}
}
$toshow = ! empty($children) ? $children : $parents;
$sep = '';
if ( ! empty($toshow) ) { foreach( $toshow as $cat ) {
echo $sep;
echo '<span class="' . $cat->name .'">';
echo '<a href="' . get_category_link($cat->term_id) . '">' . $cat->name . '</a></span>';
$sep = "https://wordpress.stackexchange.com/";
} }
}
Related Posts:
- Display current category title on category page
- Inject post (from specific category) between posts in Loop
- retrieve thumbnail from post ID of best selling product in category
- Custom HTML structure in wp_list_categories
- Allow authors to post only in categories they create in WordPress
- List all categories but exclude current post category on single post page
- Categories Template Assistance
- Edit category output
- Error: array_map(): Argument #2
- Problems with function on function.php
- Use template for posts with a particular category grandparent
- Custom Loop through category menu to include sub categories
- Different background-image by category
- Get posts with at least one category in common with current post?
- Woocommerce – remove product from category
- Get category name from custom loop and echo it once
- Display all categories including sub categories
- Custom category code not showing all posts
- hover image appears below placeholder instead of overlayed
- get current product name in functions.php
- How to use transient in this code for related post?
- Filter Select results based on selection
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to show single category archive
- Move category description below post list in blog
- How to pick the default selected value in wordpress dropdown?
- Applying A Category to Existing Posts Where Page Title Matches Regex
- Get page that displays all children of taxonomy parent
- Setting default category base on theme activation
- Hide subcategories (widget)
- Let Users Choose Post Categories
- How to show only subcategories in parent category not parent category?
- how to show only specific category for a template
- Display most popular posts of category
- Creating sub-categories via php
- get taxonomy thumbnail and use it as a variable in code
- Displaying posts based on category
- Get categories names as an array to use it in theme settings
- How to get the last category name of a child category?
- Use get() method to grab all categories and output inside another method
- Only show size attributes in product box woocommerce when available
- Argument for if term-> have child?
- 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]
- How to check if a category has a parent and child categories?
- How To Pass Array To get_the_category_by_ID() and Get An Array Back?
- Using multiple variables to assign categories to an array
- 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
- Display Sibling WooCommerce Product Categories on Archive Page
- Pagination at category doesnt work with same name of page
- display the months of a specific category
- Add Custom Category Description When Empty
- Add a product category to a specific product via a button
- List sub categories and corresponding posts of a parent category
- 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
- Create category after theme setup and modify the default one
- How to Link to category titles if it is in the single post content?
- How can i show specific Category List?
- How to show category just on specific pages?
- How to filter specific element of an array in wordpress/php?
- All categories options or All categories not Populating
- Displaying 3 Category Posts differently
- PHP get_category() function redeclared
- List categories assigned to a post
- Unable to get Post Category Name and URL
- 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
- 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
- Showing Subcategory’s Name and Link Instead of Parent Category’s
- get_category only returning details for ‘uncategorized’
- How to change links in the 3 main categories, on widget category, to a javascript function call
- how to set category name for a post
- 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
- Is there a way of displaying related posts from lowest hyrachical order (lowest child category to parent category)