- This topic has 7 replies, 3 voices, and was last updated 3 years ago by David.
-
AuthorPosts
-
April 11, 2021 at 10:01 pm #1730484Simon
Hey Guys,
I am tying to add a Custom ID to the featured Image via this code (found in here):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 quite 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 regards
April 11, 2021 at 10:08 pm #1730487ElvinStaffCustomer SupportHi there,
Are you trying to add the
id=""
attribute directly to the img element or its fine if it’s just the parent element?Also, are you trying to apply it on WPSP post images only?
Let us know.
Note: there’s no filter for the img element on WPSP, but there’s one on its parent element
<a>
. See the source code here: https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/inc/functions.php#L191April 11, 2021 at 10:12 pm #1730490SimonHey Elvin,
Are you trying to add the id=”” attribute directly to the img element or its fine if it’s just the parent element?
-> Directly to the img element
Also, are you trying to apply it on WPSP post images only?
-> No, i am trying to apply it NOT to the WPSP Post images.
April 11, 2021 at 10:58 pm #1730528ElvinStaffCustomer SupportAh i see.
Let’s try a different filter. Try
post_thumbnail_html
.Try this out:
function add_id_to_featured( $html ) { $id = get_post_thumbnail_id(); if(is_single()){ $html = str_replace( 'src=', 'id="mycustomid-'.$id.'" src=', $html ); } return $html; } add_filter( 'post_thumbnail_html', 'add_id_to_featured' );
Replace
mycustomid-
with your preferred id name. This code is structured so there’s a unique ID suffix because your ID attribute should always be unique. If you’re going to usecustom_id
w/ nothing unique in it, the ID fails is purpose and most likely won’t work.April 11, 2021 at 11:35 pm #1730570SimonOh, now that you say it, it makes sense.
Since I need a non-unique identifier, could this code also be used to add a CSS Class to the img element?
And I’m afraid I have to tell you that the code also applies the custom ID (featured-image-s42) to the WPSP Images. (See Screenshot)
Thank you so far 🙂
SimonApril 12, 2021 at 12:29 am #1730616ElvinStaffCustomer SupportAh I see. I think we need to use get_queried_object_id() and use it to check the current queried object’s ID and only apply if the current query(the current page) is equal to a reference.
Try this.
function add_id_to_featured( $html ) { $id = get_post_thumbnail_id(); $currentquery = get_queried_object_id(); $postID = get_the_ID(); if( is_single() && $currentquery == $postID){ $html = str_replace( 'src=', 'id="mycustomid-'.$id.'" src=', $html ); } return $html; } add_filter( 'post_thumbnail_html', 'add_id_to_featured' );
See it in action here: Sandbox site
April 12, 2021 at 1:14 am #1730645SimonThank you so much Elvin, works so far!
Is it also possible to add a custom CSS class with this code snippet?
April 12, 2021 at 6:42 am #1730952DavidStaffCustomer SupportHi there,
you should be able to do this:
function db_custom_id_class_first_featured_img($attr) { global $wp_query; if ( 0 == $wp_query->current_post && is_single() ) { $attr['class'] .= ' my-custom-class'; $attr['id'] = 'mycostumid'; } return $attr; } add_filter('wp_get_attachment_image_attributes', 'db_custom_id_class_first_featured_img' );
It will target just the first featured image of the single post.
-
AuthorPosts
- You must be logged in to reply to this topic.