在 WYSISWG 编辑器(高级自定义字段/WordPress)中将 class 添加到图像
Add class to image in WYSISWG editor (Advanced Custom Fields / WordPress)
我想在高级自定义字段的 WYSISWG 编辑器字段中为我 post 的每个图像添加一个 class。除了其他编辑器 classes.
之外,每个图像都应该有 class img-fluid
如果我在普通的 WordPress 编辑器中添加图像,我已经设法添加了 class。这是我 functions.php
:
中的代码
/* Add img-fluid to images in the_content */
function add_image_responsive_class($content) {
global $post;
$pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i";
$replacement = '<imgclass=" img-fluid">';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'add_image_responsive_class');
但是该代码不适用于高级自定义字段的 WYSISWG 编辑器字段。
有没有其他方法来解决内容中的图像?
您还应该使用过滤器 acf_the_content 启动您的操作,它应该可以正常工作 (https://www.advancedcustomfields.com/resources/wysiwyg-editor/)
我想在高级自定义字段的 WYSISWG 编辑器字段中为我 post 的每个图像添加一个 class。除了其他编辑器 classes.
之外,每个图像都应该有 classimg-fluid
如果我在普通的 WordPress 编辑器中添加图像,我已经设法添加了 class。这是我 functions.php
:
/* Add img-fluid to images in the_content */
function add_image_responsive_class($content) {
global $post;
$pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i";
$replacement = '<imgclass=" img-fluid">';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'add_image_responsive_class');
但是该代码不适用于高级自定义字段的 WYSISWG 编辑器字段。 有没有其他方法来解决内容中的图像?
您还应该使用过滤器 acf_the_content 启动您的操作,它应该可以正常工作 (https://www.advancedcustomfields.com/resources/wysiwyg-editor/)