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

Remove P tags from Advanced Custom Fields images

我使用的是 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', '', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

似乎没有任何效果。我怎样才能做到这一点。

顺便说一句,我使用的是 ACF Pro 的 WYSIWYG 和 JointsWP 入门主题,我的图像没有包裹在 link 标签中,如果有区别的话。

由于您使用的是高级自定义字段,因此您应该可以使用 get_field(),但请关闭格式设置:

echo get_field('myField', $post->ID, false);

详细了解 get_field() in the ACF Docs

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

remove_filter('acf_the_content', 'wpautop');
$('p > img').unwrap();

纳夫说。

这对我有用 - 我在我的函数文件中添加了它,以便在使用高级自定义字段所见即所得时摆脱图像周围的 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', '', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '', $content);
}
add_filter('acf_the_content', 'filter_ptags_on_images');