You should use get_the_category() instead of get_categories().
The get_the_category()
function will return only categories assigned to the post, while get_categories()
that you use returns all existing categories.
Edit:
To get categories assigned to post, but only those with the ancestor Food
, you can use code:
$args = [
'object_ids' => get_the_ID(), // only categories assigned to current post
'child_of' => {ID_of_food_cat} // limit results to children (descendants) of this category
];
$cats = get_categories($args);
ID of parent category can be hard-coded or dynamic:
$food_cat_id = get_category_by_slug('food'); // <-- slug of Food
$food_cat_id = ($food_cat_id instanceof WP_Term) ? $food_cat_id->term_id : 0;
Related Posts:
- Is There a Difference Between Taxonomies and Categories?
- WooCommerce: List All Categories
- Get rid of WordPress category, tag and author archives?
- How can I hide a category from Contributors in the edit/add new post screen?
- Filtering categories in the permalink structure
- Category page doesn’t use category.php, instead it redirects to homepage
- Multiple post categories – single permalink
- Wrap Post count in a span
- wp_nav_menu not appearing for a couple pages
- How to get category id of current post?
- Create product category and keyword search form in woocommerce? [closed]
- Trash bin for categories
- Display one category in one page
- How to get next previous category in same taxonomy?
- Exclude most recent posts from specific category in wp_query()
- Display all posts with same title
- Show children of top level category only
- is_archive() content being shown instead of is_category()
- Check is category parent or not from its ID
- How to display a description of a given category from a custom taxonomy
- Add custom taxonomy under the category title
- Retrieve all posts within tag OR category?
- Include template before a loop
- How to customize tag and category post listings to show introductory content?
- Include posts from feature category in pre_get_posts
- Create blog page only to see one category
- Looking for the best solution to build this Blog structure? Picture attached?
- How Can I Display Categories Description
- Specified number of categoy posts not showing correctly
- Display Slug instead of Name
- Set Current Category to Active in category.php
- Class active for Archive link – looped out
- Filtering multiple categories via the URL
- Remove sub-menu item from admin menu
- WordPress query via checkbox form
- Multiple categories with common subcategories
- Counter in loop in foreach
- How to limit number of attachments
- Show category name and new articles and display page numbers. What function to use
- Add child category to parent on post creation and save
- wp_list_categories in in nav_menu
- Sub category templates
- How to use an array of categories as a dropdown?
- ACF add custom fields to categories and display
- Custom taxonomy template for categories
- How to add custom color fields to the category edit page?
- How to create a custom categories page
- Select posts from Category
- Target a specific category from a custom taxonomy?
- Get post related on category
- How to modify the output of wp_terms_checklist when used within the built-in category metabox on edit posts?
- Searching for a category returns nothing if category is empty
- Category page returns 404 error
- How to get nav to show current_page_parent class when on regular post (not blog), differentiated by category
- How to exclude posts from category posts count
- Random category with recent post title and thumbnail
- I would like to feature a tip on the homepage
- Redirect depending on category chosen
- Problem: retrieving parent category’s first post
- Category URL Management
- How do I export the headlines of all the blog posts in a single category?
- how can we bring category metabox to Media Library/Upload New Media Page
- How can I display a list of categories of my posts
- Category post count is not correct
- 404 on category URL
- Problem creating wp categories
- List of post categories only associated to another custom taxonomy
- List all categories and count number of posts within based on custom date
- Pagination for custom categories template
- Category URL’s 404 after setting category base to ‘.’
- How to allow each editors to only edit certain categories?
- Bulk assign posts to a category using SQL (MySQL)
- sort post category listing
- Restrict retrieved terms by category?
- How to get WooCommerce Product Categories and its children’s children?
- Get category from slug list
- Media Library Category Exclude Tree
- When category name and title are the same … WP will select archive.php instead of single.php
- Re-coding category-template.php to go to custom URL
- Category removal – Portfolio Category:
- How to display child-category PAGES on a category page
- How get all media from a posts category by db?
- Cant get unique_array() work on get_the_category() foreach loop
- Show One Level Category id
- get_categories returns top-level categories instead of child categories
- Show posts from all categories
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Custom filters inside a specific category?
- how to Specifies an author / editor to edit one category only?
- Show one same template for two category
- My Query is getting the wrong data
- Formating content in category.php
- How to use Greek characters/letters in a query?
- Getting categories to change appropriately when using $cat = ‘ ‘;
- Display post content from category name using ajax
- why is the delete option missing from just one category?
- How to display the category featured images [closed]
- Echo Category In Loop
- Function to reference post meta in place of category Woocommerce
- Category Redirection Problem