This snippet in theme’s functions.php works (source & explanation)
function add_image_class($class){
$class .= ' additional-class';
return $class;
}
add_filter('get_image_tag_class','add_image_class');
But, if I try to add
$additional = get_post_meta($post->ID, 'my-varible-name', true);
if($additional != '') $class .= ' '.$additional;
return $class;
no any image code at all being inserted into post.
If you will not get the answer here, try to ask this question on http://wordpress.stackexchange.com/
Here is the code in my functions.php file I should have added it in the original post sorry
function be_attachment_field_credit( $form_fields, $post ) {
$form_fields['be-custom-class-name'] = array(
'label' => 'custom class',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_custom_class_name', true ),
'helps' => 'Custom class for images',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );
function be_attachment_field_credit_save( $post, $attachment) {
if( isset($attachment['be-custom-class-name'] ) )
update_post_meta( $post['ID'],
'be_custom_class_name', $attachment['be-custom-class-name']
);
if( isset( $attachment['be-custom-class-name'] ) )
update_post_meta( $post['ID'], 'be_custom_class_name',
( $attachment['be-custom-class-name'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
Hello, SadNinja! I understood that you used this code from WPBeginner, and this code works well. The problem is: how to add image custom meta as an img class.
It seems possible to achieve by filter get_image_tag_class
or get_image_tag
as it described in this article. But when I try to add image custom meta value to the filter function, the “insert image” button doesn’t insert any html into post.
And… Where is do you want to get this class? In a post editor?
I took a screenshot of where I want to enter the name of the class using the field I created in the tutorial here
I would like the class to be on the images that gets pulled from a list of items via a category. I have a bunch of projects and I want to give them different hover effects based on the type of project it is. This is example of the shortcode that I am using to display the projects
[catlist thumbnail=yes thumbnail_class=categories-image numberposts=50 id=11]
the id is based off the category they are in but I want to have different effects for things in the same category. I may be barking up the wrong tree as to how to go about this, but from my Google searches I thought this was the right route. Again thanks for your help, this has been kicking my butt all day.
As I see, you are using the plugin List category posts. To achieve the task in this case is impossible without an editing of the plugin code.
But it quite easy to do on standard WP archive pages.