Possibly Related Posts
- Free WordPress Theme: ‘Just Write’ September 30, 2011
- Free WordPress Theme: ‘Instapic’ July 5, 2011
The need to prevent images from displaying in the post content came up with some feedback to my Instapic theme. The user was using the WordPress iPhone App, and while making a picture post, it automatically adds the image into the post content. Since the theme uses just the featured image feature of WordPress to display the image, having the image inserted in the post content made it appear twice in the theme. He didn’t mind the need to just delete the inserted image from the post content, but I can see where that could eventually get annoying, and I want the theme to ultimately work as flawlessly and easily as possible.
The function below prevents images from being displayed in the post content when it’s placed in the functions.php file of your theme, and the_content(); is changed to the_content_no_images();
// REMOVE IMAGES FROM POST CONTENT DISPLAY
function the_content_no_images() {
$content = get_the_content('');
$content = apply_filters('the_content', $content);
$pattern = '/()/';
$replacements='';
$removed = preg_replace($pattern, $replacements, $content);
echo $removed;
}
For some reason, my blog is removing part of the code. But put < i m g . + ? > with the spaces removed inside the parenthesis in the $pattern
It isn’t without it’s imperfections, as the image is still technically in the post content and would be displayed in the feed (unless you change it to show Summary instead of Full text)—it’s just prevented from being displayed, which does the trick in my case.
Note: This post is over a year and a half old. Although it's not a fresh post, feel free to comment or share your thoughts anyway. I read and enjoy every comment that is posted here.