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
- Missing feature image link function
- Add image size if page template
- Set JPEG compression for specific custom image sizes
- Display random categories on the front page (Finding and Editing Theme Functions)
- Issue with wp_get_attachment_image() and SVG file type
- How to change the markup WordPress inserts for post images
- How to add a class to the attachment images
- Programmatically Set First Image as Featured
- 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?
- WP 4.4. responsive loads normal image after loading the responsive image
- How to create thumbnails for PDF uploads?
- current_cat_ancestor Alternatives
- How to reduce original image quality on upload?
- Add body class of category parent
- Is it possible to maintain image aspect ratio when scaling images?
- Set image size on media and text block with a function
- Generating Responsive Background Image Sizes in PHP
- 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
- Add custom field to attachments in a specific category
- Create thumbnail on PDF upload with Gravity Forms
- Gallery images titles – get from post
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- How to set child post categories to parent post categories when updating parent 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
- Insert width & height attributes to all images displayed on each page
- How To Get WordPress Categories Link List?
- Converting a simple plugin to be placed inside of functions.php
- Custom image size in Media Dropdown
- Limit function to specific post category
- Removing the first image in content
- Function to auto-set a featured image that is already in use
- Actions according to image type and size
- Changing post category from dropdown
- Manipulate Output of wp_list_something: select menu instead of li’s
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- Make Categories a Dropdown Menu in Media Library
- i can’t use wordpress functions in ajax loaded php file
- 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
- Printing medium sized image
- restrict uploaded image size and fixed image display size
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class
- require_once() Causing categories and tag pages on dashboard to not refrsh
- Using Switch Statement to Change Image According to Last Digit of Topic ID
- Disable links to images only if link is an image
- different body classes for each category
- Restricted images only for logged user. (hidden url)
- Zip all (original) images from media gallery
- 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?
- Set thumbnail from URL, by grabbing image in functions.php
- Dynamically adding Captions to images
- How to get tags and categories?
- Full size image option removing problem
- Function to get thumbnail img source
- How to Insert A List of Posts in A Category Written by the Author into the Author Archive
- Image width issue in IE [closed]
- How to link images of one WP site to another WP site?
- image_size with respect to aspect ratios
- Removing all Category pages with one exception
- How to resize image from import image url?
- How to crop image in WordPress with aspect ratio for any size of image
- How to add height and width dimensions automatically to archive descriptions
- How to overwrite image if it already exists – WordPress, Gravity form
- Get category of post inside save_post hook
- Disable Attachment Page Except for Category
- I am looking for a function to create category links in the wordpress loop for articles
- How Do I Prevent Featured Image Caption Displaying Above And Below Image
- Override categories with Pages (block theme)
- How to force Cover block to use the full size image resolution
- warning: trying to access array offset on value of type bool ACF field image group
- Add custom text before all image captions in WordPress posts
- Hreflang Tags from the Most Recent Post Appearing in Category Page Headers
- add_image_size() parameter four ($crop = true) is ignored: the_post_thumbnail() returns scaled image(s) instead of hard cropped