Xóa thẻ p xung quanh thẻ img trong WordPress

Cập nhật lần cuối 20/08/2015 by trong WordPress vào 20/08/2015 có 1913 Views

Bình thường khi thêm ảnh, WordPress sẽ tự động thêm thẻ <p> xung quanh thẻ <img>.

Ví dụ:

blah blah blah
<img src="monkey.jpg">
blah blah blah

Khi xuất ra bài viết:

<p>blah blah blah</p>
<img src="monkey.jpg">
<p>blah blah blah</p>

Chúng ta có thể fix nó bằng hàm sau. Thêm hàm này vào file functions.php của theme nhé

//Xóa thẻ <p> xung quanh thẻ <img>
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');

Sau khi xóa chúng ta sẽ được:

<p>blah blah blah</p>
<img src="monkey.jpg">
<p>blah blah blah</p>

Chúc các bạn thành công!

Tham khảo thêm : https://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/

5/5 - (2 votes)
Từ khóa:
  • Bình luận
Sản phẩm nổi bật của Toản
x