gpt4 book ai didi

php - 将类 img-responsive 添加到 wordpress 中所有附加的帖子图像

转载 作者:行者123 更新时间:2023-11-28 04:34:30 33 4
gpt4 key购买 nike

我想将 class=img-reponsive & img-shadow 添加到所有附加的帖子缩略图我使用了以下功能,它工作正常,但它删除了 thubmails 的原始类

function add_responsive_class($content){

$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));

$imgs = $document->getElementsByTagName('img');
foreach ($imgs as $img) {
$img->setAttribute('class','img-responsive img-shadow');
}

$html = $document->saveHTML();
return $html;
}

但我想合并我的类,而不仅仅是覆盖它们所以我用了jquery

 jQuery(function() {
jQuery(img).addClass('img-responsive img-shadow ');
});

但它给出错误 jquery not defined

请帮帮我

最佳答案

function add_image_responsive_class($content) {
global $post;
$pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i";
$replacement = '<img$1class="$2 img-responsive img-shadow"$3>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'add_image_responsive_class');

关于php - 将类 img-responsive 添加到 wordpress 中所有附加的帖子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23365628/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com