• Resolved Stephen B

    (@wordpmoon12)


    I have this code and it was working earlier but recently it stopped working, not sure why.

    function addclassfeaturedimage($attr) {
      remove_filter('wp_get_attachment_image_attributes','addclassfeaturedimage');
      $attr['class'] .=  get_the_ID() ;
      return $attr;
    }
    add_filter('wp_get_attachment_image_attributes','addclassfeaturedimage');

    When working it adds the post ID to the attachment image class.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi WordpMoon12

    Your code works just fine for me. Why are you removing the filter inside the callback function? I don’t think selectors can start with a digit. Try it with a different selector with post id in it.

    function addclassfeaturedimage( $attr ) {
    	$attr['class'] .= ' post-id-' .  get_the_ID();
    	return $attr;
    }
    
    add_filter( 'wp_get_attachment_image_attributes', 'addclassfeaturedimage' );

    Now you can add css for the post like this

    .post-id-234 {
    
    }

    Thread Starter Stephen B

    (@wordpmoon12)

    Great thanks keesiemeijer, that code fixed the problem!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding post ID as a class to attachment images’ is closed to new replies.