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?
<?php // 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 https://code.google.com/p/timthumb/ 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]; $str1=trim($str1); $len=strlen($str1); $imgpath=substr_replace(substr($str1,5,$len),"",-1); ?> <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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Strip ALL text from posts, only display attachment’ is closed to new replies.