Support » Plugins » Hacks » Strip ALL text from posts, only display attachment

  • Hello,

    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 };
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    It does remove images, but then displays thumbs of the images at the end. Reversing this is not just a matter of changing if == to if !=, it’s pretty much a rewrite. In your case, there’s a better way, IMO.

    Define a function that gets the ID of the current post and makes a new query for all (image I assume) attachments to that post. It then loops through each attachment and generates the necessary HTML to display the associated images.

    On the applicable template, replace calls to the_content() with calls to your image attachment query function.

    Thanks bcworz. That is exactly what I’d like to do.

    Just a couple of questions…
    In which file do I define the function that gets the id of the current post?

    I assume on the template I would replace the_content() on the single.php, yeah?

    Thanks again.

    Moderator bcworkz


    Your new attachment query function would go in functions.php. The call to this function does indeed replace the_content() on single.php and anywhere else you actually want only images and not text.

    If you haven’t already done so, create a Child Theme so your modifications are protected from being overwritten during a theme upgrade.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Strip ALL text from posts, only display attachment’ is closed to new replies.