How to add a CSS class to every image in a Custom Post Type

Combining the answer here by @cjcj with the code in this answer, the code that works for me outside the loop, in functions.php is:

// Add ability to check for custom post type outside the loop. 
function is_post_type($type){
    global $wp_query;
    if($type == get_post_type($wp_query->post->ID)) return true;
    return false;
}

// Add class to every image in 'wpse' custom post type.
function add_image_class($class){
    if ('wpse' == is_post_type()){
        $class .= ' additional-class';
    }
    return $class;
}
add_filter('get_image_tag_class','add_image_class');

Leave a Comment