![](/img/trans.png)
[英]Adding Bootstrap img-responsive class to Wordpress images fails at gallery thumbs
[英]add class img-responsive to all attached post images in wordpress
我想將class = img-responsive和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未定義
請幫幫我
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');
為什么不隨便用函數重新添加原始類呢?
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','thumbnails img-responsive img-shadow');
}
$html = $document->saveHTML();
return $html;
}
如果使用上面提到的DOMDocument方法,這可能會導致utf編碼出現問題,尤其是在通過內容編輯器使用特殊字符時,例如:£轉換為? 或其他奇怪的字符。 您可以遍歷$ img,然后在$ content上使用str_replace,但是使用“ terminators”方法對我來說要好得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.