Php 用自定义div替换wordpress博客文章图像标记
wordpress博客上发布的图片被标记为Php 用自定义div替换wordpress博客文章图像标记,php,html,css,wordpress,Php,Html,Css,Wordpress,wordpress博客上发布的图片被标记为 <figure class="wp-block-image size-large"> <img src="http://localhost/ela/wp-content/uploads/2020/07/MG_5512_23.jpg" alt="" class="wp-image-541" srcset="http://localhost/ela
<figure class="wp-block-image size-large">
<img src="http://localhost/ela/wp-content/uploads/2020/07/MG_5512_23.jpg" alt="" class="wp-image-541" srcset="http://localhost/ela/wp-content/uploads/2020/07/MG_5512_23.jpg 683w, http://localhost/ela/wp-content/uploads/2020/07/MG_5512_23-200x300.jpg 200w" sizes="(max-width: 683px) 100vw, 683px">
</figure>
我希望将图像封装在以下结构中
<div class="img-box">
<div class="item loaded">
<img src="http://localhost/ela/wp-content/uploads/2020/07/07_40.jpg" data-src="http://localhost/ela/wp-content/uploads/2020/07/07_40.jpg" alt="title">
</div>
</div>
我发现了这个问题
但我无法形成所需的正则表达式来实现我想要的。我在文章中试过的代码是
function breezer_addDivToImage( $content ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$replacement = '<div class="myphoto">$1</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
add_filter( 'the_content', 'breezer_addDivToImage' );
function breezer\u addDivToImage($content){
//查找内容的正则表达式。
$pattern='/(]*)>)/i';
//替换内容。$1指的是上面括号中第一个“捕获组”中的内容
$replacement='$1';
//在$content上运行preg_replace()
$content=preg_replace($pattern,$replacement,$content);
//返回已处理的内容
返回$content;
}
添加_过滤器('the_content'、'breezer_addDivToImage');
您能发布您尝试过的内容吗?@disifor,我尝试过这个方法,但我无法计算出我案例的正则表达式。您能将代码添加到您的问题中吗?如果这个链接失效,这个问题对未来的访问者将毫无用处。