- This topic has 12 replies, 3 voices, and was last updated 2 years, 11 months ago by Elvin.
-
AuthorPosts
-
June 19, 2018 at 9:35 pm #604080forgemedia
Is there an easy way to add a custom class to featured images on blog posts? For example, take my post here: https://woorkup.com/happyforms-vs-contact-form-7/
Currently, it looks like this:
<img width="1588" height="796" src="https://woorkup.com/wp-content/uploads/2018/05/contact-form-7-vs-happyforms-1.png"......
I would like it to look like this:
<img width="1588" height="796" class="mycustomclass" src="https://woorkup.com/wp-content/uploads/2018/05/contact-form-7-vs-happyforms-1.png"......
I am using the Code Snippets plugin π Perhaps some quick PHP?
Thanks!
June 19, 2018 at 11:30 pm #604109forgemediaNevermind… realized there was a
"attachment-full size-full"
further down I could target πI’m all good!
June 20, 2018 at 9:10 am #604513TomLead DeveloperLead DeveloperGlad you found a solution π
You can always do this as well:
.featured-image img
May 27, 2020 at 1:03 pm #1304019AndyI would love a way to actually do this (to add a class not just select it for JS/CSS purposes)
May 27, 2020 at 1:29 pm #1304058LeoStaffCustomer SupportAre you referring to the featured image on the posts page or single posts?
May 27, 2020 at 1:30 pm #1304059AndyHey Leo, on the single posts page
May 27, 2020 at 1:41 pm #1304077LeoStaffCustomer SupportDoesn’t look like there is a filter there unfortunately.
What are you trying to achieve?
Just wondering if there is a workaround.
May 27, 2020 at 1:56 pm #1304091AndyI want to add either a class or data attribute to the feature image (so I can exclude the image from being lazy loaded in A3 lazy load)
May 27, 2020 at 4:10 pm #1304191TomLead DeveloperLead DeveloperHi there,
You could try this:
add_filter( 'wp_get_attachment_image_attributes', function( $attrs ) { if ( is_single() ) { $attrs['data-something'] = 'my-value'; } return $attrs; } );
April 11, 2021 at 2:04 pm #1730251SimonHello to all,
sorry to wake this old Post, but I didn’t want to open up a new one. I am looking for the exact same function, but I am not able to do it correctly.I tried this code:
function wpzeus_filter_gallery_img_atts( $atts, $attachment ) { global $post; if ( is_single() && (isset($atts['class'])) && (($attachment->post_parent) === ($post->ID)) ) { $atts['id'] = 'mycostumid'; } return $atts; } add_filter( 'wp_get_attachment_image_attributes', 'wpzeus_filter_gallery_img_atts', 10, 2 );
The problem is the following: I am using wp show posts (pro) to display some posts in my sidebar. This function is applying the mycustomid to all the posts shown by wp show posts AND the featured image.
-> Since I need the id to exclude only the featured image, it would be kind of ineffectiveI already tried to filter it by logical operators, but I couldn’t find something.
Also I can’t use the selector (.featured-image img), because “rendering” it requires JS code, which doesn’t make sense when looking for page speed.
Is there any solution to add a custom ID attribute ONLY to the featured image of the current single post?
Thanks in advance and best ragards
April 11, 2021 at 2:28 pm #1730258LeoStaffCustomer SupportHi Simon,
Can you actually open a new topic for this?
Thanks π
April 11, 2021 at 10:02 pm #1730486SimonApril 11, 2021 at 10:08 pm #1730488ElvinStaffCustomer SupportHi Simon,
Please see my reply to your topic:
https://generatepress.com/forums/topic/add-custom-image-id-to-featured-images-and-nowhere-else/#post-1730487Thanks. π
-
AuthorPosts
- You must be logged in to reply to this topic.