0

To generate a thumbnail for certain part of my theme i'm using the following code :

if(has_post_thumbnail() && !post_password_required() && !is_attachment() &&  wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()))):
    $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()));
    $thumbnail = get_the_post_thumbnail(get_the_ID() );
else:
    $thumbnail_url[0] = get_template_directory_uri(). '/assets/images/no-image.jpg';
    $thumbnail = '<img src="'.esc_url(get_template_directory_uri(). '/assets/images/no-image.jpg').'" alt="'.get_the_title().'" />';
endif;
echo '<div class="post-thumbnail">'.$thumbnail.'</div>';

I would like to be able to add a class and an id from the post it's attached to to my code on this line $thumbnail = get_the_post_thumbnail(get_the_ID() ); but I can't figure out how to modify the code accordingly.

I want to be able to add some lightbox options to my thumbnails.

1 Answer 1

2

The third parameter you can pass to get_the_post_thumbnail is $attributes, which you can use to pass custom HTML attributes including classes.

$thumbnail = get_the_post_thumbnail(
    get_the_ID(), // Post ID.
    'thumbnail', // Image size.
    array( // Attributes
        'class' => 'my-custom class-names', // Classes go here.
    )
);

Note that this will override the default image classes that WordPress assigns (which is attachment-$size_class size-$size_class)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.