Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have decided that for all pages where multiple posts are shown (e.g. front, category, tag pages), that post images should not be shown. Instead, featured images, if specified, should be shown next to each post.

Right now, featured images are working fine for posts. However, when multiple posts are displayed (e.g. on the front page, category pages), images should be stripped from the posts.

When a single post is displayed, the images should display as well.

I'm assuming that this has something to do with add_filter() and a regex str_replace() on the post, but this would affect the post no matter where it's displayed.

Reply

share|improve this question
1  
Share the code that you have tried so far? –  Chittaranjan Dec 24 '13 at 6:55

2 Answers 2

up vote 0 down vote accepted

Try this

<?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;
}
?>

Place in your functions.php file, call up your the_content() like normal. This will allow you to still have a feature image, but remove all images from main page. If you want to remove them from other pages just add to your and ||

--Edit--

You were on the right track with the filter and replace, :) just have to do some digging.

share|improve this answer
    
Well since I have the 'selected answer', then I see your problem is resolved. What I meant by doing some digging, is just going to Google and searching your problem at hand. Look through the first few pages, if you don't find it right away, search for the same thing, but word the search differently. I found your answer in a matter of 1 minute on Google. I did not come up with the code myself, I simply found it, and then shared it. Never take the 'lazy' way out, as GBMtoan posted earlier. The lazy way isn't always the correct way, and the correct way is the best way! –  MrJustin Dec 25 '13 at 1:20
    
If you have any other problems or questions, simply reply and I will help you out the best that I can.. happy coding! –  MrJustin Dec 25 '13 at 1:21

Simple way I do. I set feature images for each post.

global $post;
// number post; category Id.
$args = array( 'numberposts' => 4, 'offset'=> 0, 'category' => 4 );

$myposts = get_posts( $args );

foreach($mypost as $post){

if(has_post_thumbnail($post->ID)){ 

// Get an Array; Array[0]: url of feature image; Array[1]: width; Array[2]: height

$arr_image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
}

Image Url :  $arr_image_info[0]"

do somthing if you want....
}

Getting the first image match in post and display it - is other way. But maybe difficult. That way is good for lazy post-er :)

Hope can help you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.