从高级自定义字段中删除P标签图像
问题描述:
我使用的是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入门主题,我的图像不包含在链接标记中,如果这有所作为。
答
$('p > img').unwrap();
这份厚礼说。
答
由于您使用的高级自定义字段,你应该能够使用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');
答
这为我工作 - 我说这在我的函数文件摆脱使用先进的自定义字段所见即所得当周围图像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');
为什么不禁止'wpautop'完全? – rnevius
我试过了:remove_filter('the_content','wpautop');但这不起作用,我认为,因为我没有使用the_content();即时通讯使用高级自定义字段wysiwyg [the_field('myField'); ] 对? – agon024