Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 添加类img响应wordpress中所有附加的帖子图像_Php_Html_Css_Wordpress - Fatal编程技术网

Php 添加类img响应wordpress中所有附加的帖子图像

Php 添加类img响应wordpress中所有附加的帖子图像,php,html,css,wordpress,Php,Html,Css,Wordpress,我想将class=img responsive&img shadow添加到所有附加的帖子缩略图中 我使用了下面的函数,它工作得很好,但是它删除了thubmails的原始类 function add_responsive_class($content){ $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); $document = new DOMDocument();

我想将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没有定义

请帮我解决

函数添加\u图像\u响应\u类($content){
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');
全球$员额; $pattern=“//i”; $replacement=''; $content=preg_replace($pattern,$replacement,$content); 返回$content; } 添加过滤器(“内容”、“添加图像”类);
函数添加图像响应类($content){
全球$员额;
$pattern=“//i”;
$replacement='';
$content=preg_replace($pattern,$replacement,$content);
返回$content;
}
添加过滤器(“内容”、“添加图像”类);

为什么不用函数重新添加原始类呢

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;   
}

为什么不用函数重新添加原始类呢

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”方法对我来说效果要好得多。

如果您使用上述DOMDocument方法,这可能会导致utf编码问题,特别是在通过内容编辑器使用特殊字符时,例如:£被转换为?或者其他奇怪的角色。您可以循环使用$img,然后在$content上使用str_replace,但是使用“terminators”方法对我来说效果更好。

您是否将
jQuery
库放在主题的头文件的第一位,你能确定
jQuery
确实被加载了吗?你是否在主题的头文件中首先包含了
jQuery
库,你能确定
jQuery
确实被加载了吗?