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)
- Print Current Post Index number within Loop
- How to add category to: ‘wp-admin/post-new.php’?
- Use category base slug in posts’ permalink
- Display the latest post from a category in a page
- rewind_posts() – what actually the use of it, and where using is required or preferred?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- WordPress Number of Posts Not Changing With posts_per_page
- Add default content to post (for specific category)
- Find most recent authors
- Query only Posts from Both of Two Category?
- Display most recent post in category instead of archive?
- How to show related posts by category or custom post type?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- get_post_type_archive_link(‘post’) returns current category archive
- Get user’s most published categories
- Converting multiple loops into one single loop with pagination
- How to set post expiration date and time and move the page to archive after expiration [closed]
- How would I get 1 latest post from a query for 5 posts?
- Exclude posts from featuring
- How To Remove Certain category Posts From the Blog Page
- Add icon/badge next to posttitle of specific category
- Saving category to a post, before publishing the post
- Assign category to new post via URL
- Stop users posting into category while still seeing contents of the category
- Category Page Template not Showing Posts
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- Post Loop Missing Most Recent Post
- Categories list into registration form
- Change all author links in Blog roll
- Change default category when I publish a post
- Loop doesn’t exclude the specified category in home page
- Post Image not displaying in category view
- WordPress Query Posts From Category Post on Static Page
- Loop through posts? Want to remove extra newlines in actual post HTML/content on backend
- WP_Query: Show 10 posts in date order, first three random
- How include css class based on post ( in loop ) slug?
- WordPress Loop: How to display recent posts in multiple divs
- Cannot implement byline into posts container in single.php
- How to check if there are posts with the same tag
- Unable to differentiate between two categories under custom post type on single.php
- Exclude post category in a blog page
- How come this loop is not working? [closed]
- How to make posts being uncategorized
- What’s a theme that properly handles previews of different types of posts?
- Using the loop to set locations for all posts on a single google map
- How to add content above footer in posts from specific category
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- Shortcode for Comments and Comment Boxes in Post Loop
- Permalink Short code showing unnecessary link text inside the loop
- How to Create another Page Category like the “Post” and “Pages”
- How to create a button to filter posts (list) by meta value
- How to display particular categorie’s post which associated to specific user?
- Display Specific Categories posts on the home page
- Code to display random ordered posts in Categories across pages [duplicate]
- Double loop output
- AJAX load more posts not using correct category and repeating the same few posts
- Category posts show on local install, they do not show on live server
- how to print total number of posts filtered by category?
- post->ID displays the same ID number for every post
- Add category information beneath post?
- Post are not showing up
- Newly created category does not show category
- Show a Category Specific Info Box
- How to inject a post within a loop
- Display the first post’s comments of category in comments.php template
- How to filter categories of a post
- List posts related to category on a div [closed]
- how to show all post with its contents
- posts_per_page – Repeats only first posts ‘post__in’ array
- How to display my categories in a list (to a post, not the sidebar)?
- How to make category for word post_content