This should hopefully do the trick:
/**
* Append the image categories to the current image class.
*
* @see http://wordpress.stackexchange.com/a/156576/26350
*/
add_filter( 'get_image_tag_class',
function( $class, $id, $align, $size )
{
foreach( (array) get_the_category( $id ) as $cat )
{
$class .= ' category-' . $cat->slug;
}
return $class;
}
, 10, 4 );
Testing (WP 3.9.1):
If we set the image categories as (for example):
rolling-stones, steel-wheels-tour, wembley
Screenshot:
and then insert the image into the post, we get the extra classes as expected:
category-rolling-stones category-steel-wheels-tour category-wembley
Screenshot:
Related Posts:
- How to add a rel attribute to images that contains their categories?
- remove links from images using functions.php
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- Best way to programatically add “rel” attributes to page and post images
- Image rotation fails to regenerate custom sized thumbnail
- Display images that are not in the content
- How to hide image-url if no attachment?
- What are the meta fields for an attachment?
- Get Attachment Category Name
- Different image using srcset function
- Auto delete content in specific folder inside media library
- How to update an image attachment’s alt text from a custom field when saving a post?
- Open image size links in a page instead of direct image link
- Add image size if page template
- Display random categories on the front page (Finding and Editing Theme Functions)
- How to add a class to the attachment images
- Removing default image size list in Media Box
- How to use get_template_directory_uri() to load an image that is in a sub-folder of my theme?
- How to create thumbnails for PDF uploads?
- Is it possible to maintain image aspect ratio when scaling images?
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Why are image thumbnails cropped proportionally (not per dimensions)?
- 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
- How to Acheive the custom woocommerce category template
- Gallery images titles – get from post
- Assign category using custom field?
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Display height and width properties with the_post_thumbnail() or related function
- Limit function to specific post category
- Function to auto-set a featured image that is already in use
- Actions according to image type and size
- Changing post category from dropdown
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
- Find first image on paginated post for Pinterest
- Attach parent category template to all subcategories
- How can you limit srcset on a single type of page?
- exclude a category from a search on a specific page
- Custom HTML in specific category single page and its descendant categories
- Possible to display shortcode based on the category?
- Select pages by category
- Overwriting Parent Theme’s Image Sizes
- Twentyten Child Theme: header images display messed up in admin panel
- Disable auto-resizing of uploaded images, but only for certain filename
- Set spesific size of featured images
- Get attachments for posts that belongs to a specific category
- media resize/crop possibilities?
- Redirect to another page using contact form 7? [closed]
- Get category URL for current post
- allow only one post in specific category
- Random Featured Image Based on Category
- How to obtain the recent posts without their content in an efficient way?
- Running script based on Category
- Way to store featured image width and height in two separate variables?
- Limit dimensions of “Full” size images
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- attachment page template? only show attachments for current post?
- Access category within rss2_head hook?
- issue with if/elseif in_array inside foreach loop display only one post
- Randomizing wp_tag_cloud() in child theme
- add_filter to specific WooCommerce Category
- How to rewrite wordpress search to work on specific category
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- Image loading function not working on archive.php template
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- How to Add Shortcode to html img code?
- Specify multiple categories for custom post template – FATAL ERROR
- How to add a new image size and apply it to posts only?
- Problem in using Customizer
- Custom Image Size in Insert Media
- get_image_tag() html output : empty src attribute
- Need To Resize Images Exactly Without Losing Image Details
- Get slug of current category in functions.php
- Custom image sizes showing in Classic Editor only when upladed directly to post
- how can I change all wordpress media file url to custom cdn url?
- Display a list of random terms from custom taxonomy with shortcode
- WP_Query for Attachments not working as expected
- Remove the ‘category’ url for one category type
- How do i tweak my wp Post title base on category of the post
- blank page with wp_get_attachment
- how to remove a tag in the_category function
- Menu and category highlighting for a single post
- Remove all video attachment, both files and post/postmeta from database?
- Featured image on archive page based on post type
- Graphic before title – Specific Category
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Different body image backgrounds on different pages, posts and categories
- Update add_image_size
- Function the_content
- How To Get WordPress Categories Last Update Date?
- Dynamically adding Captions to images
- Full size image option removing problem
- Removing all Category pages with one exception
- How to resize image from import image url?
- How to add height and width dimensions automatically to archive descriptions
- How to overwrite image if it already exists – WordPress, Gravity form
- Disable Attachment Page Except for Category