I’m trying to add a CSS class to every image in a custom post type.

I’ve found this answer, to add a class to every image in general:

function add_image_class($class){
    $class .= ' additional-class';
    return $class;
}
add_filter('get_image_tag_class','add_image_class');

How would I build on this, so that it only applies to a custom post type?

2 Answers
2

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 Reply

Your email address will not be published. Required fields are marked *