Just use the break
statement to abort after the first cat that is found and that is a child category.
foreach ( get_the_category() as $cat )
{
if ( cat_is_ancestor_of( 10, $cat ) )
{
printf(
'<a href="https://wordpress.stackexchange.com/questions/103533/%s">%s</a>',
get_category_link( $cat->cat_ID),
echo $cat->cat_name
);
break;
}
}
Related Posts:
- How to check if I’m on the last page of posts?
- Multisite – Protect categories from deletion?
- Where/how are categories stored?
- Get top parent category
- wp_update_nav_menu_item() to insert categories
- Redirecting specific category posts to subdomain
- How can I prevent users from creating new categories?
- Get child categories when clicking on parent category
- Custom color for each category name
- Block multiple categories from Blog
- Best and Cleanest way to remove /category/ from category permalinks? [duplicate]
- Show posts of one category only with Custom Taxonomy on single.php
- Wrap a chosen category name with div
- What type of page should i use? static page or category page? and what’s the difference? [closed]
- Listing Parent, Child and GrandChild Categories and then the PostTitles on Page Template !
- Category tree is flattened inside admin upon saving
- Get woocommerce catogry
- Categories vs. Page Site Organization
- Get last post date yesterday from each categories not work & double result post
- How do I add a function on a specific post category?
- Open subcategories in submenu on click in primary menu
- How to add Categories to the top menu of the website?
- Multiple categories PROBLEM and Duplicating main categories
- force category base – archive pages work WITH & WITHOUT category base slug in url?
- Retrieve Image from Media Library by it’s category
- Getting URL of archive (category) page
- Strip a word from wp_list_categories
- Exclude category from fucntion
- Get Unique Categories – Group By?
- Filter Categories in meta box for CPTs
- Site search needs to find posts for category topic AND Tag or keyword thats not in the post
- Get category url in loop
- Previous/next post links displaying wrong category than is desired
- List only child categories within posts
- Admin comments and user restrictions
- category url structure
- Order by category titles
- How to conditionally hide author name on Single.php if category is “news”, otherwise if category is something else display author name?
- How to let users choose posts order in categories?
- WordPress category
- Different Output for “Every Other” Foreach Statement?
- And/Or Category Query
- Show subcategories on a parent category page
- How do I add Category Body Classes to Admin?
- Is there a reason I cannot get the current category in a loop?
- How to get parent category ID in single page template
- “Item not updated” when try to change category properties
- How can I tell what category I’m in– in the header?
- How to display sticky or latest published post on the current category page it belongs to?
- List just subcategory and products of active category page in Woocommerce
- Custom Meta Box (SELECT2) Not Saving Taxonomy Terms
- How do I add a separator to my list of terms with get_category
- Listing posts under a category by date
- Display content from a specific category using cat ID not working
- Trying to create a Categorised Index (archive?) to use as my Home Page
- Filter posts by categories ajax is showing nothing
- How can I add a class to displays a category?
- Deleting a category from WordPress admin does it remove it completely from the database?
- WP_Query – using category__and when one of the categories is 0
- Hide categories from frontend, but not admin / post editor
- Exclude categories from homepage not longer working
- Post to inherit custom category background image from parent
- Related posts widget – category
- Last x Posted Categories?
- Check boxes for showing categories
- Header Link function not working. Problem with bloginfo()
- WordPress category filters as subcategories
- Is there a “sensible” limit to WordPress Categories?
- Possible to make get_categories() hide_empty attribute honor excludes from post query?
- Display Category Name and Date Posted on Blog Post
- How to remove “Categories:” part after the post title? [closed]
- Please tell how to stop the category from showing up on posts
- Editing categories crashes WordPress site
- Remove “category” from permalink with add_rewrite_rule
- Next and previous links category infinite loop
- Conditional Site Logo(Category Based)
- show only terms from parent category
- Tag Menu items based on their categories
- Accessible Subcategory URL’s pages without using .php hack
- How to display total views of all posts of one category?
- in_category(‘category-sluga’) returns true for Archive page of Category ‘category-slugb’
- How to exclude a specific categogy from a custom page template
- wp_list_category only showing to logged in uers
- Grouping parent categories into sections
- How to show “category” name?
- Category Templates for Post Types not Working
- Change category base based on category
- Querying Multiple Categories With Different Offsets
- filter single_cat_title avoiding the breadcrumbs
- htaccess redirect for all categories converted to tags now showing 404
- altering theme – content generated by PAGES not PORTFOLIO
- Category page with gallery for each post
- Need help with category page customization meeting certain condition
- How do i add custom post types to this query?
- add new input field like category?
- WordPress excerpt for specific posts in category
- How do I get a single page navigation depending on the previous page?
- chose category in plugin
- How to define category ID in an array?
- sub-category showing up in wrong order in breadcrumbs