Wordpress 从高级自定义字段图像中删除P标记

Wordpress 从高级自定义字段图像中删除P标记,wordpress,image,tags,Wordpress,Image,Tags,我使用的是WordPress 4.2.2,每次我向WYSIWYG添加图像时,它都会将输出图像包装在段落标记中。我需要去掉这些标签。我在网上找到的所有东西都是从2011年开始的,而且似乎都不起作用 我尝试过在functions.php中添加以下内容: function filter_ptags_on_images($content){ return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\

我使用的是WordPress 4.2.2,每次我向WYSIWYG添加图像时,它都会将输出图像包装在段落标记中。我需要去掉这些标签。我在网上找到的所有东西都是从2011年开始的,而且似乎都不起作用

我尝试过在functions.php中添加以下内容:

function filter_ptags_on_images($content){
  return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
图像上的函数过滤器($content){ 返回preg_replace('/\s*()?\s*()\s*()?\s*/iU','\1\2\3',$content); } 添加_filter(“_内容”、“过滤器_ptags_on_图像”); 似乎什么都不管用。我怎样才能做到这一点


顺便说一句,我使用的是ACF Pro的WYSIWYG和JointsWP启动程序主题,如果有区别的话,我的图像不会包装在链接标记中。

因为您使用的是高级自定义字段,所以您应该能够使用
get_field()
,但请关闭格式设置:

echo get_field('myField', $post->ID, false);
阅读更多关于

您还可以通过在functions.php中执行以下操作来删除
wpautop
的ACF实现:

remove_filter('acf_the_content', 'wpautop');

nuff说。

这对我很有用-我在我的函数文件中添加了这个,以便在使用高级自定义字段WYSIWYG时去掉图像周围的p标记:

// gets rid of p tags around images when using the WYSIWYG advanced custom fields

function filter_ptags_on_images($content)
{
    $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('acf_the_content', 'filter_ptags_on_images');
//在使用所见即所得高级自定义字段时,去掉图像周围的p标记
函数过滤器对图像进行过滤($content)
{
$content=preg_replace('/\s*()?\s*()\s*()?\s*()?\s*/iU','\1\2\3',$content);
返回preg_replace('/\s*(*)\s*/iU','\1',$content);
}
添加“过滤器”(“acf内容”、“过滤图像上的标签”);

为什么不完全禁用
wpautop
呢?我已经尝试过:删除过滤器('the_content','wpautop');但我认为这不起作用,因为我没有使用_content();我使用的是一个高级自定义字段wysiwyg[the_field('myField');]对吗?实际上,这是在剥离所有的p标记,即使是在我需要将它们包装在段落标记中的段落中。我只想把它从图片上去掉
// gets rid of p tags around images when using the WYSIWYG advanced custom fields

function filter_ptags_on_images($content)
{
    $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('acf_the_content', 'filter_ptags_on_images');