Zamalek Man
Zamalek Man

Reputation: 309

Removing Images from a WordPress Post

I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post which had an amazing little piece of code that solved my problem.

<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
echo $postOutput;
?>

But some time i have link above images like:

<a href="#"><img src="PATH_IMAGES"></a>

So, How can remove it?

Upvotes: 1

Views: 77

Answers (1)

Ashish Patel
Ashish Patel

Reputation: 3614

Change above code by following code which remove link if exist into your content.

$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);
echo $postContent; 

Upvotes: 2

Related Questions