Home › Forums › General Issues › adding a class to image attachment
Hi All,
I want to add a classname to the image that I am getting with wp_get_attachment_image
I use the standard ACF snippet like so:
$image = get_field('image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
Now I want to add a classname like so:
$image = get_field('card1_image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
$class='rounded-top'
if( $image ) {
echo wp_get_attachment_image( $image, $size, array ('class' => 'rounded-top' ) );
}
But no class is given to the image.
What am I doing wrong?
Thanks.
Ah, I mixed things up.
This is what I have now:
$image = get_field('card1_image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size, array ('class' => 'rounded-top' ) );
}
and that does not seem to work…
Looking at your code and the code for the WP function I only see one thing that could be causing this.
https://developer.wordpress.org/reference/functions/wp_get_attachment_image/
Question: is the class populated with anything?
Possibly there is a filter on ‘wp_get_attachment_image_attributes’
Hi John,
Question: is the class populated with anything?
I use the class in other places on the site if that is what you mean?
Possibly there is a filter on ‘wp_get_attachment_image_attributes
mmhh, I looked for something like that but could not find anything. Are you saying the code is correct and should work?
I use <?php the_post_thumbnail('large', array('class' => 'rounded-top')); ?>
in other places on the site and that works. My rounded-top class is added to the wp-post-image class.
I tried using
$image = get_field('card1_image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size, array ('class' => 'rounded-top' ) );
}
In twenty-ten and it does not work either. The only classes I see is attachment-full size-full
In the ACF field settings I use the “ID” since I want the srcset thing.
Thanks
OK Found it.
As per the Code reference, the function uses four values:
wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )
Where I use only three. If I add the fourth value like so:
echo wp_get_attachment_image($image, $size, "",array('class' => 'rounded-top') );
It works. So problem solved.
What makes it hard to spot the fault is that WP seems to use four values where in this case the four values seem required?
Anyway, thanks for looking into this and pointing me toward the code reference.
sorry, duh on my part, I completely missed the fact that you didn’t have the right number of arguments in your call.
@hube2 , hi trying to do this to apply a class to woocommerce single product gallery thumbnails. i’m using a true/false acf field on media attachment.
this code is not working for me:
<?php
$image = get_field('quick_ship');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size, array ('class' => 'quick-ship' ) );
}
That is the same issue, you need 4 values, not 3.
So try this:
$image = get_field('quick_ship');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size, "", array ('class' => 'quick-ship' ) );
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.