2015-07-20 158 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'); 
相關問題