You can do something like this:
function category_has_parent($catid){
$category = get_category($catid);
if ($category->category_parent > 0){
return true;
}
return false;
}
and use it like this:
if (category_has_parent('22')){
//true there is a parent category
}else{
//false this category has no parent
}
Update:
to check the other way around (if a category has children) you can use get_categories
$children = get_categories(array('child_of' => id,'hide_empty' => 0));
if (count($children) > 1){
//has childern
}else{
//no children
}
Related Posts:
- Get rid of WordPress category, tag and author archives?
- How to add the category ID to admin page
- How to create a widgetized sidebar for every category dynamically?
- Only show category to certain user levels without plugin
- List Categories of the Parent Category of the Current Category
- exclude category from WordPress Form function
- wp_list_categories, Add class to all list items with children
- Remove “Parent” Selection When Adding/Editing Categories?
- How to show in search results posts with a particular tag in a particular category?
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- If category is in parent category?
- If in category to be inside of a function
- Rename Default Category (Uncategorized) Via a Function
- Check child/parent categories if exists
- Getting URL of archive (category) page
- How to show only one category in breadcrumb navigation
- Query posts from category based on a filter most favorited
- Add tag parameter to category.php
- Random Featured Image Based on Category
- add slug beside name in Admin Category Checklists
- Change div background acording to the selected category
- How to use wp_list_categories with plugin category?
- Redirect depending on category chosen
- How to add icon over specific thumb
- How to load several hierarchical categories from functions.php
- Change Default Content when Creating a Post based on Previous Category Choice
- How to add a custom thumbnail size for a specific category?
- create function to call category name and slug
- Different size video display for category page (smaller) & detail page (larger)
- Code to display category-specific single.php is overriding normal single.php, too
- Loop Through Categories in Custom Field
- Function to list posts from current post’s category fails in WP 3.8
- Show only one category in main query, issues on tag page
- Show parent-child relationship for categories in the wordpress admin
- Get category ID from category slug (not working)
- How do I remove “Uncategorized” from posts with more than 1 category?
- Remove parent category from permalink? Basically only have the child category?
- Filter by post category in Media Library
- Custom post types with their own permalink structure
- Send an email when custom post type category is changed
- Display the deepest child category from Category X (in loop)
- Exclude subcategories from the url
- Why adding Categories does not auto refresh in Backend while using my custom theme?
- Removed slug from CPT, now How/where do I hook the filter to the taxonomy term archive pages link?
- Remove specific category from a post
- Search by word, category, tag, author
- How to hide certain categories in wordpress dashboard?
- If a post belongs to two categories how do I choose the main category?
- Change post category based on latest posts views
- How to get the depth of a category’s subcategories
- in_category() works in single.php but not in page.php?
- functions.php conditional tag – only show in single.php?
- How do I create a way for users to assign categories to a post from the frontend of the website?
- Get categories from save_post hook
- Display children of the category a post is assigned to
- Random post + categories + tags
- Show Primary Category first when I display post categories
- WordPress admin categories sort show is wrong
- Displaying Categories and Sub Categories
- Change thumbnail image depending product category
- How to list categories within tags?
- get_next_post set the category
- WordPress URL Rewrite for dynamic and customized URL
- Different menu navigation per category
- Get category URL knowing it’s id
- Targeting Parent Category Pages
- Change every post background based on category
- How could I know all the categories from post?
- Get category’s parent category while using get_the_category_list
- How to show the category of custom taxonomy
- Show testimonials based on post category matching product name
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- Remove parent category from child/sub-category page URL in WordPress
- Add code to header based on category for specific posts?
- How to only show parent categories in sidebar
- Can I prioritize post categories for choice for breadcrumb trail?
- Custom Portfolio Page
- Custom Fields Value As Taxonomy
- Disable Category Taxonomy
- How do you get sticky posts to work with categories?
- A different permalink structure for different categories
- How to make product image inherit from category image
- Is it possible to set up blog categories without changing permalink structure
- How to display categories image in loop homepage WordPress
- How to “remove” WordPress categories UL and make the list loop in a horizontal line?
- How do I rewrite a single category link to point to a custom page?
- Add subcategories posts to the counts column at the admin’s categories list
- Default category isn’t saved in database
- Display related posts with same category or same tag return blank
- Get posts from 2 or more categories
- date.php shows all posts. how to fix it?
- Help with Wp + Mixitup code
- Matching usermeta at registration to categories of content
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- show posts from some category and sort by advance custom field
- How to show monthly archive posts?
- How to get specific number of category post on a page or category page?
- Use same wordpress category base and tags as the prefix as the post permalink
- Display different sidebar based on search results from one category
- Adding category to existing post (uploaded file)