2015-07-20 68 views
0

我使用的是WordPress 4.2.2,每当我将图像添加到WYSIWYG时,它都会将输出图像包装在段落标签中。我需要去掉这些标签。我似乎在网上找到的所有东西都是从2011年起加上它似乎并不奏效。从高级自定义字段中删除P标签图像

我试图把事情在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'); 

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

顺便说一句,我使用的是ACF Pro的所见即所得和JointsWP入门主题,我的图像不包含在链接标记中,如果这有所作为。

+0

为什么不禁止'wpautop'完全? – rnevius

+0

我试过了:remove_filter('the_content','wpautop');但这不起作用,我认为,因为我没有使用the_content();即时通讯使用高级自定义字段wysiwyg [the_field('myField'); ] 对? – agon024

回答

0
$('p > img').unwrap(); 

这份厚礼说。

1

由于您使用的高级自定义字段,你应该能够使用get_field(),但关闭格式化:

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

了解更多关于get_field() in the ACF Docs

您还可以通过执行删除的wpautop的ACF实施的functions.php如下:

remove_filter('acf_the_content', 'wpautop'); 
+0

这两个工作完美。谢谢! – agon024

+0

实际上,这是剥离了所有的p标签,即使在我需要它们被包裹在段落标签中的段落中。我只是想从图像中删除它 – agon024

0

这为我工作 - 我说这在我的函数文件摆脱使用先进的自定义字段所见即所得当周围图像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');