admin管理员组文章数量:1130349
I have a function like this :
function add_responsive_class($content)
{
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
if (!empty($content)) {
$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-fluid');
}
$html = $document->saveHTML();
return $html;
}
}
add_filter('the_content', 'add_responsive_class');
this adds img-fluid to all the images but also it removes the align-left , how can i modify the above function to add img-fluid to image instead of it removing the other classes?
like --> <img class ="img-fluid align-left"
I have a function like this :
function add_responsive_class($content)
{
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
if (!empty($content)) {
$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-fluid');
}
$html = $document->saveHTML();
return $html;
}
}
add_filter('the_content', 'add_responsive_class');
this adds img-fluid to all the images but also it removes the align-left , how can i modify the above function to add img-fluid to image instead of it removing the other classes?
like --> <img class ="img-fluid align-left"
1 Answer
Reset to default 0$img->setAttribute('class', 'img-fluid');
This sets the class field to a new value. Simply change this line to
$classes = $img->getAttribute('class');
$img->setAttribute('class', $classes . ' img-fluid');
I have a function like this :
function add_responsive_class($content)
{
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
if (!empty($content)) {
$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-fluid');
}
$html = $document->saveHTML();
return $html;
}
}
add_filter('the_content', 'add_responsive_class');
this adds img-fluid to all the images but also it removes the align-left , how can i modify the above function to add img-fluid to image instead of it removing the other classes?
like --> <img class ="img-fluid align-left"
I have a function like this :
function add_responsive_class($content)
{
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
if (!empty($content)) {
$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-fluid');
}
$html = $document->saveHTML();
return $html;
}
}
add_filter('the_content', 'add_responsive_class');
this adds img-fluid to all the images but also it removes the align-left , how can i modify the above function to add img-fluid to image instead of it removing the other classes?
like --> <img class ="img-fluid align-left"
1 Answer
Reset to default 0$img->setAttribute('class', 'img-fluid');
This sets the class field to a new value. Simply change this line to
$classes = $img->getAttribute('class');
$img->setAttribute('class', $classes . ' img-fluid');
本文标签: theme developmentadd class to all images inside the content
版权声明:本文标题:theme development - add class to all images inside the content 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749111094a2317402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论