最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

images - Remove the inline styling added in wp_get_attachment_image()

programmeradmin5浏览0评论

Here is how I am calling wp_get_attachment_image:

$id = attachment_url_to_postid( get_theme_mod( 'blue_logo' ));
echo wp_get_attachment_image($id, $size = 'blue_logo', $icon = false, array(
   "class" => "blue_logo"
))

'blue_logo' size is referencing an image size that I am creating in functions.php. Here is what the output element looks like.

<img width="216" height="67" src="http://34.219.141.113/wp-
content/uploads/2022/02/Q7_WHM_HOR_BLUE-1.jpg" class="blue_logo" alt="" loading="lazy" 
srcset="http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1.jpg 2062w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-300x93.jpg 300w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1024x317.jpg 1024w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-768x238.jpg 768w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1536x476.jpg 1536w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-2048x635.jpg 2048w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1568x486.jpg 1568w" sizes="
(max-width: 216px) 100vw, 216px" style="width:100%;height:30.99%;max-width:2062px;">

I understand that I can override the styling with CSS, but when I do that it's causing some compression which is causing the image to lose quality. Is it possible to not have the inline styling at all in the first place? My ultimate goal here is to be able to display an image that is being uploaded via a custom WP_Customize_Image_Control and set the image size with as little image compression/loss in quality possible.

Thank you for the help!

Here is how I am calling wp_get_attachment_image:

$id = attachment_url_to_postid( get_theme_mod( 'blue_logo' ));
echo wp_get_attachment_image($id, $size = 'blue_logo', $icon = false, array(
   "class" => "blue_logo"
))

'blue_logo' size is referencing an image size that I am creating in functions.php. Here is what the output element looks like.

<img width="216" height="67" src="http://34.219.141.113/wp-
content/uploads/2022/02/Q7_WHM_HOR_BLUE-1.jpg" class="blue_logo" alt="" loading="lazy" 
srcset="http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1.jpg 2062w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-300x93.jpg 300w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1024x317.jpg 1024w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-768x238.jpg 768w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1536x476.jpg 1536w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-2048x635.jpg 2048w, 
http://34.219.141.113/wp-content/uploads/2022/02/Q7_WHM_HOR_BLUE-1-1568x486.jpg 1568w" sizes="
(max-width: 216px) 100vw, 216px" style="width:100%;height:30.99%;max-width:2062px;">

I understand that I can override the styling with CSS, but when I do that it's causing some compression which is causing the image to lose quality. Is it possible to not have the inline styling at all in the first place? My ultimate goal here is to be able to display an image that is being uploaded via a custom WP_Customize_Image_Control and set the image size with as little image compression/loss in quality possible.

Thank you for the help!

Share Improve this question asked Feb 18, 2022 at 21:11 BeccaNBeccaN 171 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You could try using get_attachment_image_url() instead. This will give you the url for the full image. You can

Call it like this:

echo '<img class="blue_logo" src="'.get_attachment_image_url($id, 'blue_logo').'">';
发布评论

评论列表(0)

  1. 暂无评论