When i post something with an image, its also displayed over homepage.. And my post has image some images inside the content of the post.. and I am not using any featured image..

How can we display on the custom fields on home screen.. like I want to display 'Post title' and 'Post Date' and some 'post content' without images with limited characters.

And when i click on post title, it should redirect to the original post where it has all details of the post.


This should help you..

function remove_images( $content )
    //Run only on the front page, in your case the homepage
        //Remove the images
        $postOutput = preg_replace('/<img\b[^>]++>/i','', $content);

        //Get the first 200 characters only, you can change the number if you want
        $postOutput = wp_html_excerpt($postOutput, 200);

        return $postOutput;

    return $content;

add_filter( 'the_content', 'remove_images', 100 );

Put this code in your functions.php file.

| improve this answer | |
  • That regex is broken. At least the closing > should be added, plus some other tweaks: /<img\b[^>]++>/i to make it less bad. HTML and regex remains a quirky marriage. – Geert Dec 12 '11 at 15:51
  • Thanks for letting me know this! Actually I copied the regex image removal from some blog. Corrected it now! – Rutwick Gangurde Dec 12 '11 at 15:52

the_excerpt() will pull content from your post to where ever without pulling in any of the images associated with the post. It is not default behavior to post images into when using the_excerpt() unless you force it to do so.

More about Images in Excerpts

Then as requested in the simplest terms, you'd put something like this inside your home page template. (home.php or index.php or front-page.php)

        <?php if ( have_posts() ) : ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <h2><a href="<?php the_permalink(); ?>"><?php the_title();  ?></a></h2>
                <p><?php the_date(); ?></p>
                <?php the_excerpt(); ?>
                <p><a href="<?php the_permalink(); ?>">read more about <?php the_title(); ?></a></p>

            <?php endwhile; ?>

        <?php endif; ?>
| improve this answer | |

If you don't mind using a plugin, Advanced Excerpt will take care of most of this for you. It allows you to select which tags, including <img>, appear in excerpts (or don't).

You do have to be showing excerpts for this to help, of course - make sure your index.php is calling the_excerpt(); instead of the_content();.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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