Use get_ancestors()
to get the parent terms. Here is an excerpt from my plugin T5 Parent Terms in body_class:
$ancestors = get_ancestors(
get_queried_object_id(),
get_queried_object()->taxonomy
);
if ( empty ( $ancestors ) )
{
return $classes;
}
foreach ( $ancestors as $ancestor )
{
$term = get_term( $ancestor, get_queried_object()->taxonomy );
$new_classes[] = esc_attr( "parent-$term->taxonomy-$term->slug" );
}
This will work with any taxonomy, not just categories.
Related Posts:
- Manipulate Output of wp_list_something: select menu instead of li’s
- Setting a default text for excerpts of a particular category
- Graphic before title – Specific Category
- How to Insert A List of Posts in A Category Written by the Author into the Author Archive
- Define custom Page Template without its own .php file
- Removing default image size list in Media Box
- How to restrict actions and filters “properly” by conditions
- ajax category filter
- syntax for remove_filter in parent theme with class
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Override parent theme function that is not hooked or in the functions.php file
- Php string not working in WordPress Functions.php (trying to fetch 1st category for each blog that post appears in the sidebar)
- Add class to Categories Widget
- Remove bulk actions based on user role or capabilities
- How to Acheive the custom woocommerce category template
- Removing title from page
- Hide some items from Screen options in dashboard for products
- Assign category using custom field?
- Replace Archive Widget Link Text
- How To Get WordPress Categories Link List?
- How to make unique add_filter to the_content of specific page template files – so each template gets its own addition
- Best way to programatically add “rel” attributes to page and post images
- Limit filter upgrader_post_install to a single plugin
- Changing post category from dropdown
- How to update feed only 2-3 times a week (for Feedburner email)?
- Remove function or filter
- Filter categories of posts with checkboxes
- Get the category name outside of the loop in category.php
- Adding multiple taxonomy filters to functions.php
- Adding HTML to the end of every post with the Block Editor
- Unset Category if other Category is unset during post transition
- Only let plugin add actions to wp_head & wp_footer on single posts
- Show Primary Category first when I display post categories
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Run wp_kses_decode_entities on atom feed?
- Filtering out the #more anchor link that gets produced by
- append to existing parent theme function
- Get category URL for current post
- allow only one post in specific category
- How can I add a filter for specific categories on functions.php?
- How to add custom JavaScript in functions?
- Running script based on Category
- How would I go about replacing this function in my child theme located in inc/template-tags.php
- Retrieve a value from Yoast SEO to use to set a default twitter card image honoring overrides
- How to add_filter html template to middle of content
- Filter an WordPress Function in (general-template.php)
- Redefine function arguments before rendering
- How can I add a class to a nav li depending on URL?
- Access category within rss2_head hook?
- Randomizing wp_tag_cloud() in child theme
- Modify gform_other_choice_value for specific form and specific field in Gravity Forms
- Combine embed_oembed_html and oembed_result
- login_headertitle is deprecated since version 5.2.0
- Set “woocommerce_is_purchasable” to false for specific “$product->is_stock_status”
- Filter to wp_list_authors
- Image loading function not working on archive.php template
- “All posts” in the category widget
- Add Adsense code between job listings – wp job manager plugin
- Problem with images URL after filter applying
- remove_action not working, even after changing priority [duplicate]
- remove/hide wp-editor
- Replace header image on all other pages but home – URL issue
- Add a class to post if it has been recently updated
- search form leads to 404
- how to add_filter to non hook function
- Change Default Content when Creating a Post based on Previous Category Choice
- Get slug of current category in functions.php
- how can I add filter in specfic field in my website?
- Display a list of random terms from custom taxonomy with shortcode
- Change category display name function
- Overwrite text in a complicated filter hook
- How to add HTML into error message
- Save_post – Warning: Cannot modify header information
- custom error message for empty username and password using authenticate filter not working
- Add #primary at the end of navlink permalinks on single posts
- remove_filter function to unhook twentysixteen parent theme function
- only update titles of single posts
- OOP Switch statement with array as parameter
- How can I automatically delete comments that contain a URL?
- Appending „read more” to the excerpt conditionally
- How to add class dynamically from templates
- how to remove a tag in the_category function
- function replace_text for entire page
- add product thumbnail to checkout page only and include variation name
- ACF Filter return value
- Which method is more correct for removing WooCommerce Extensions menu item?
- How to make an If Else on Excerpt Filter
- Why might wptexturize have become re-enabled?
- Regex works in regexr, but not if I filter content [closed]
- Retrieve a custom form field modified by a filter
- functions.php filters not applied in AJAX call
- Settings in functions.php used by a plugin
- Can’t change excerpt length and more tag
- Force resize for all video content
- Way of getting queried loop before the query with a filter hook?
- Trying to get this function to show below the content
- Removing all Category pages with one exception
- Warning: call_user_func_array() expects parameter 1 to be a valid callback
- How can I modify or filter this variable in an existing class? (Mai Theme)
- Get category of post inside save_post hook