This takes all the categories associated with your post and checks if they are parents of anything else. If one is not, it is the lowest category and is echoed out.
<?php
$postCats = wp_get_post_categories();
foreach($postCats as $childcat) {
//this is a top level category
if ($childcat->category_parent==0) {
continue;
}
for ($J=0;$J<sizeof($postCats);$J++) {
//if another category lists it as its parent, it cannot be the lowest category
if (strcmp($childcat->name,$postCats->category_parent)==0)
break;
}
//at this point, no other cateogry says it's its parent, therefore it must be the lowest one
echo '<li> <a href="'.get_category_link($childcat->cat_ID).'">';
echo $childcat->cat_name . '</a> </li>';
}
?>
Related Posts:
- Display all posts from specific categories on a page
- How to show list of posts by author and category?
- Divide Loop Into Days & Categories
- How to filter posts that belong to a specific category only if that is the only category
- Display post category in foreach loop with category link
- Child_of not displaying all children posts.. via get_posts
- Exclude category from
- Pulling in post category and children within category
- How to make the first post in the loop be styled like a “new / featured” post?
- How do I stop the loop from repeating in my category template?
- get_the_category listing in hierarchial order
- why does wordpress ignore the post args?
- Showing categories and subcategories with posts
- WP_Query of Category Not Showing First Post
- Get post category as a separate string and url
- posts going to 2 pages/categories only show up in 1
- Get current taxonomy and display query accordingly
- Only the most recent post is showing on my category page (working on localhost, not live site)
- Current post categories and subcatecories outside of the loop
- How to distinguish pages created by woocommerce?
- How to show category image if no featured image is set?
- Display new posts categories in separated divs
- List Authors For Current Category
- Multiple Post Repeat for Related Post Loop by Category
- Show post if in category
- Displaying categories items among posts
- Make assigning post to a specific category equivalent to assigning it to all categories
- Show all posts in category
- View post with specific category id and name which I selected in the backend (drop-down option)
- First x post with another template then the others
- How to get subcategories from category slug?
- Remove current category from post but display all others
- Loop doesn’t show title of second post and posts thereafter
- How show categories in admin and get that selected to show posts in index
- Change all author links in Blog roll
- Grid post page on WordPress with Bootstrap, the_excerpt(); Problem
- Second get_permalink inside loop points to wrong URL
- Change default category when I publish a post
- Get the amount of posts on a current page
- Loop parent terms {display posts} AND loop child terms {display posts}
- How to add a class to each individual post?
- store posts_id of category into a varable?
- add bootstrap post slider with tabs
- How to display two random-post sections that are each under their own category
- How can you display all sibling categories to a post?
- How to add add more properties to WP_Post object in search results loop
- Loop doesn’t exclude the specified category in home page
- Custom taxonomy archive page problem
- Display posts of specific category term
- Widget that shows categories with posts numbers
- How to create a sub post?
- Post Image not displaying in category view
- Loop don’t work within single.php page
- Use Click Image to Play Youtube Video in a WordPress Loop
- How do I show posts from another wordpress installation?
- older blog posts not showing in new theme
- How to get posts published on the latest date?
- get posts from Custom Post Type & Category
- The_excerpt() doesn’t parse – how to change that?
- Trying to edit archive.php to only show post extract, with featured image
- Category page when using static front page
- Add Blog to WP homepage Manually
- Latest posts by category — how to exclude current post?
- How can I remove the first two words and shorten get_the_title()
- How to only publish posts with image in it
- How to show all posts by author on buddypress profile with navigation
- Page and post loop same template
- How do I include the category next to the title of a post?
- Relative number of post in category
- WordPress Query Posts From Category Post on Static Page
- How to create a “latest news” page showing a list of posts from blog category
- WordPress bulk category select when publishing post
- ACF to select posts not displaying on blog page
- Exclude some categories from listing on the current post
- Add custom text automatically on each post
- Getting blog pagination to work on page set as front page
- How to show a custom taxonomy in the theme?
- How to import nested categories from XML file?
- Extracting relevant tags associated with that particular single post only
- Make loop inside slider divisible [closed]
- how to hide empty fields of post category description?
- how can i display my posts alphabetically?
- How to add automatically bootstrap 4 order-lg-1 and order-lg-2 classes for columns in foreach loop based on the count?
- Why this function doesn’t works without the_post?
- if in category but only with post meta
- How to get post with associated categories and tags names instead of ids with rest api?
- Display current post position in Elementor Posts widget
- Display a mixed list of posts and media
- Migrate posts from category and sub-category via SQL
- Parent category / child category posts
- Posts Missing in Dashboard after update
- How to enable custom Shortcodes in Post Category Description? [duplicate]
- Sort by last word in title
- Loop through posts? Want to remove extra newlines in actual post HTML/content on backend
- Display post number by category
- WP_Query: Show 10 posts in date order, first three random
- singular posts using archive styling
- Change Post Title For Specific Category
- How include css class based on post ( in loop ) slug?
- Display selected categories onto post page