Post Pic

How to: Prevent post images from being displayed on homepage

When using images in your posts, you not always want them to be shown on your blog homepage. In theses cases, here is a little piece of code that prevent any images from the post to be displayed on the homepage.

Simply paste this code in your functions.php file:

<?php
add_filter('the_content','wpi_image_content_filter',11);

function wpi_image_content_filter($content){

    if (is_home() || is_front_page()){
      $content = preg_replace("/<img[^>]+\>/i", "", $content);
    }

    return $content;
}
?>

Credits goes to Avice De’véreux for this awesome recipe!

13 Responses

Apr 03 2009 15:54

If you don’t use images on home page it looks to typographic.

Apr 04 2009 15:29

I prefer all my post with images (on Home page and single post) it looks nice and cool

Apr 05 2009 03:21

I think removing images from the home page is a perfect solution for small posts, blogs with dozens of new posts per day, and of course, blogspam. But I needed it for a quasi-twitter blog. It worked well.

Apr 05 2009 05:37

Nice tip.

I think some themes are designed to show images via custom field. I wish it was this easy to modify them too :)

Keep up the great work you are doing..

Apr 05 2009 08:39

Perfect, just what I needed as the images really mess up the view on home page with excerpts.

Apr 06 2009 05:43

I use a theme that not show image in the homepage. Now i know what the cause of it..

Apr 06 2009 09:20

Can you use a similar function to be able to apply timthumb to images in the content too?

Apr 12 2009 16:29

I actually made this custom filter for WP archive page (archive,date,tags,cats). The function is from http://code.google.com/p/wp-istalker/source/browse/branches/WordPress/2.7/1.6.2/lib/modules/filters.php#148

Thanks for spreading it out. Someone might find it useful

Aug 18 2009 10:51

so many useful tricks ,think you very much.

Oct 30 2009 22:28

OMG, I`ve looked everywhere for this code. Thank you !

Jan 26 2010 17:27

Would I add the enclosing <?php brackets. Or would I remove these and place anywhere on the page. Because my images are not removed. But i'd like to remove the images and use the thumbnails instead.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required