Strip ALL text from posts, only display attachment

  • Author
  • #1222346


    I would like to only show attachments from posts, not the text. I found and added this code to single.php and it does exactly the opposite – hides the post attachment (The photo) and displayes the text. It seems like this could be modified to do the opposite, but I’m not exactly sure how. It seems like there should be an easier way. What do you think?

    // Get the all post content in a variable
    $posttext = $post->post_content;
    $posttext1 = $post->post_content;

    // We will search for the src="" in the post content
    $regular_expression = '~src="[^"]*"~';
    $regular_expression1 = '~<img [^\>]*\ />~';

    // WE will grab all the images from the post in an array $allpics using preg_match_all
    preg_match_all( $regular_expression, $posttext, $allpics );

    // Count the number of images found.
    $NumberOfPics = count($allpics[0]);

    // This time we replace/remove the images from the content
    $only_post_text = preg_replace( $regular_expression1, '' , $posttext1);
    /*Only text will be printed*/
    echo $only_post_text;

    // Check to see if we have at least 1 image
    if ( $NumberOfPics > 0 )
    /* Here you can do what ever you want
    I used timthumb script to crop all the images and display in thumbnails.
    for that upload the timthumb.php file in your theme folder.

    for ( $i=0; $i < $NumberOfPics ; $i++ )
    { $str1=$allpics[0][$i];
    <img src='<?php echo get_bloginfo('template_url')."/timthumb.php?src=".$imgpath."&w=300&h=300&zc=1";?>' alt=""/>
    <?php };




    You did not specify a blog address or reason for posting when you created this topic.

    This support forum is for blogs hosted at If your question is about a self-hosted WordPress blog then you’ll find help at the forums.

    If you don’t understand the difference between and, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.

    This is an automated message.


    That’s not a free hosted blog and we can’t help you here. If you don’t have a username account at click and register one on the top right hand corner of the page that opens, so you can post to the support forums there and receive advice from bloggers.

The topic ‘Strip ALL text from posts, only display attachment’ is closed to new replies.