Requirements
For example, on a desktop device, the image uses the following HTML code:
The Code is as follows: |
|
1 |
On the mobile device side, because the screen is relatively small, if you want to make the image adaptive screen, we should remove the width and height attributes, or the image may be larger than the screen:
The Code is as follows: |
|
1 |
Method 1,
Http://www.111cn.net/wy/wordpress/58923.htm
Copy the following code to the functions. php file of the current topic:
The Code is as follows: |
|
Add_filter ('Post _ thumbnail_html ', 'remove _ width_attribute', 10 ); Add_filter ('image _ send_to_editor ', 'remove _ width_attribute', 10 );Function remove_width_attribute ($ html ){ $ Html = preg_replace ('/(width | height) = "\ d *" \ s/', "", $ html ); Return $ html; } |
Method 2
The Code is as follows: |
|
// Delete the width and height of the adaptive image, by Ludou Function ludou_remove_width_height_attribute ($ content ){ Preg_match_all ("/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png \. bmp]) [\ '| \ "]. *? [\/]?> /", $ Content, $ images ); If (! Empty ($ images )){ Foreach ($ images [0] as $ index => $ value ){ $ New_img = preg_replace ('/(width | height) = "\ d *" \ s/', "", $ images [0] [$ index]); $ Content = str_replace ($ images [0] [$ index], $ new_img, $ content ); } } Return $ content; } // Determine whether the browser is a mobile device If (wp_is_mobile ()){ // Delete the width and height attributes of img in the document content Add_filter ('the _ content', 'ludou _ remove_width_height_attribute ', 99 ); } |
In this way, I will try again to see if the desired result is reached.