admin管理员组文章数量:1435859
Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src' attributes to 'src' so my images will display. What's the way/best way to go about doing this? Would appreciate any info on this topic. Thanks.
Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src' attributes to 'src' so my images will display. What's the way/best way to go about doing this? Would appreciate any info on this topic. Thanks.
Share Improve this question asked Mar 19, 2019 at 16:16 Evan Meredith-DaviesEvan Meredith-Davies 1531 gold badge1 silver badge4 bronze badges 3 |2 Answers
Reset to default 0You can loop through all your posts and use regex to replace that string. The quick and dirty version would be to just put this in a page template, assign it to a page and then visit said page. If you want something a little cleaner then modify this code to work with something like WP Utility Script Runner.
$posts_to_clean = get_posts(array(
'posts_per_page' => -1,
));
echo 'Posts to clean: ' . count($posts_to_clean) . '<br>';
foreach ($posts_to_clean as $dirty_post) {
$dirty_post->post_content = preg_replace('`(data-src)`', 'src', $dirty_post->post_content);
$updated = wp_update_post($dirty_post, true);
if (!is_wp_error($updated)) {
echo 'Updated post ' . $updated . '<br>';
} else {
echo 'Unable to update post ' . $dirty_post->ID . '<br>';
}
}
echo 'Complete!'
You could also do this directly in your phpmyadmin by a quick search and replace:
Launch phpMyAdmin. Then, you will need to click on your WordPress database name on the left and then click on the SQL tab at the top. Enter this code (be sure to change the table prefix if yours is different)
update wp_posts set post_content =
replace(post_content,'data-src','src');
Then click "go".
And ANOTHER option! Although I don't necessarily like plugins to do this work I know that there is one called Better Search Replace that will do this from within the the dashboard with a simple GUI as well.
本文标签: databaseHow do I loopiterate through posts to edit all img tags
版权声明:本文标题:database - How do I loopiterate through posts to edit all img tags? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745674745a2669783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data-src
? – mrben522 Commented Mar 19, 2019 at 16:18