your code only works if you are using category
taxonomy.
if you are using custom taxonomy like brands
you must change your code to this:
function add_category_to_single( $classes ) {
if ( is_singular() ) {
global $post;
$taxonomy = 'brands';
$terms = get_the_terms( $post->ID, $taxonomy );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$classes[] = $term->slug;
}
}
}
return $classes;
}
add_filter( 'body_class', 'add_category_to_single' );
Related Posts:
- Custom Taxonomies in body class with parent slug
- Add category base to url in custom post type/taxonomy
- Display all posts in a custom post type, grouped by a custom taxonomy
- Custom Post Type Permalink / Rewrite not working immediately
- Ordering Posts List By Taxonomy Terms?
- has_term or in_category for Custom Post Types
- WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
- WordPress Custom Post Type Category Page
- post count is wrong when using same taxonomy for 2 different CPT
- Issues when rewrite rules collide?
- Custom-Taxonomy as categories: Remove “most-used” tab?
- Custom Permalinks for Custom Post Types and Taxonomies
- how to group custom post type posts by custom taxonomy terms
- Showing current taxonomy terms
- Unable to save custom taxonomy terms in a custom-built metabox
- Custom Taxonomy order by Custom Field
- Archive page for taxonomy of custom post type
- CPT tax slug conflict with post type slug
- Custom post type archive with dynamic taxonomy filtering – is it possible
- Exclude current post when getting related post on custom post type and taxonomy
- Display Posts of a Category in Alphabetical Order (Custom Post Type)
- Show ACF field from custom taxonomy and display on the single template
- Custom post types, taxonomies, and permalinks
- How can I display my custom metaboxes on a custom post template?
- How to specify URLS for custom posts & taxonomies
- Taxonomy.php Not Showing Posts
- Display custom post type in hierarchical order with get_terms
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- Am unable to reset a query properly
- Custom Post Type permalink shows wrong page: homepage/index.php
- using $wpdb to get custom post type with term
- Categories sorting
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Allow user to set custom order to a list of custom taxonomies?
- Disable block with taxonomies at post page
- Display Custom Toxonomy In WordPress and theme Widgets category selection dropdown list
- Sort and filter custom post type posts by custom taxonomy
- Custom Post Type Pagination Paginates Only in URL Structure
- Terms showing up in object cache on unrelated pages
- I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
- Hierarchial Custom Post Types/Taxonomies
- Hierarchical permalinks for custom post type and taxonomy
- Only show posts with date of today or in the future (i.e don’t show past posts)?
- Best way to specify “article type” in URL
- Rename a slug label
- List Posts By Custom Taxonomy
- Dynamically insert an article at the top of a taxonomy archive – or?
- WordPress multisite – is it possible to have different taxonomies for each site?
- Migrating Hierarchal Taxonomy Categories Between Post Types
- How to show list of taxonomy terms associated with specific post?
- Custom Columns for Custom Post Type Manager
- I have custom post type with custom taxonomy. But not found the taxonomy page..
- Group Custom post type in a taxonomy page by its child taxomony
- Display Taxonomy Term Children in a Drop Down without Submit Button
- Custom taxonomy query showing more than 4 posts
- Use different categories for a custom post
- Filter posts by specific custom category and current tag
- Help on Custom Post Types
- Custom category URL not displaying posts
- Display featured posts for a custom post type by taxonomy
- What is the most efficient way to execute recursive complex queries?
- 2 custom post type paging in 1 custom taxonomy
- How to display post from one custom post type in another custom post type with the same taxonomy?
- WordPress REST API V2: “{CUSTOM_POST_TYPE} matches Term ID List and Term ID Taxonomy Query, but should match only one.”
- Duplicate slug/permalink issue while adding same post name
- how to achieve this permalink abc.com/CPTName/CustomPostTypeCategorySlug/categoryname
- have to do a while have post to show for two custom post types sharing the same taxonomy?
- Remove custom permalink base from CPT custom category and custom tags
- How to handle this wordpress custom post type rewrite problem?
- Is it possible to arrange Custom Post Types from CPTUI into a Folder?
- Change Search display for Custom Post Type
- WooCommerce – Custom related product (Spareparts)
- tax_query not working?
- Issue with Custom Post Types and Permalinks
- Automatically Add a default category to a Custom Post Type before save_post action
- Add active class to foundation 6 tabs while looping categories
- Get list of all Topics in use by a custom post type
- Custom post types with child taxonomy not visible in admin
- Connect 8 Taxonomies to One (Main) taxonomy. How to?
- Filter By Term Not Working – Custom Post Type
- Custom post type category archive URL redirects to home page
- How to have permalink structure based on taxonomy terms
- Checking Taxonomy Terms for a Custom Post Type With get_the_terms Not Working
- Organizing The Custom Post Type with Taxonomies / Parent Posts
- Get posts that have custom post type value
- Taxonomy Query of Custom Post Displays Archive Instead of Posts
- Listing custom post type items from a couple of custom taxonomies
- Permalink for Custom Post Types
- Catergory args causing loop not to show
- How to Create Short Code Using Custom Post type
- Function not working on live server but every thing is fine on localhost
- How to change custom post type slug without damage seo? [closed]
- Custom Post Types using wrong template (index) instead of archive-{type}.php – previously worked as expected
- Custom post type with custom taxonomies structure url not working archive of CPT
- Make term slugs of custom taxonomy available in WP REST API for custom post type?
- An script/Plugin for automatic page creation per term
- Add Shortcode functionality to a function that queries custom post type / taxonomy
- Taxonomy term archive claims there are no posts, but there are. How to resolve?
- List all posts in hierarchy and groupped by each taxonomy
- Search for portfolio tags & mixing portfoliotags and post tags