Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom classes to "wp_get_attachment_image( $attachment_id)" wordpress function

Tags:

wordpress

I can get images using wp_get_attachment_image($attachment_id) wordpress function.It automatically add below code.

<img width="150" height="150" src="http://localhost/test2/wordpress/wp-content/uploads/2019/08/pexels-photo-1308624-150x150.jpeg" class="attachment-thumbnail size-thumbnail" alt="">

I want to add "alt" and another class using this function. To be specific I want to generate code like this.

<img width="150" height="150" src="http://localhost/test2/wordpress/wp-content/uploads/2019/08/pexels-photo-1308624-150x150.jpeg" class="attachment-thumbnail size-thumbnail custom-class" alt="Some Alt Value">
like image 856
shen4 Avatar asked Aug 01 '19 11:08

shen4


2 Answers

You can pass it like this.

<?php echo wp_get_attachment_image( $attachment_id, 'thumbnail', "", ["class" => "my-custom-class","alt"=>"some"]); ?>

You can read the full documentation here. https://developer.wordpress.org/reference/functions/wp_get_attachment_image/

like image 73
Shalin Nipuna Avatar answered Oct 07 '22 10:10

Shalin Nipuna


You can pass mulitple attributes, for a custom class you can do:

<?php echo wp_get_attachment_image($image_id, 'full', false, array('class' => 'my-custom-class')); ?>
like image 2
Loosie94 Avatar answered Oct 07 '22 09:10

Loosie94