Need help? Check out our Support site, then


Strip ALL text from posts, only display attachment

  1. 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<br /> // Get the all post content in a variable<br /> $posttext = $post->post_content;<br /> $posttext1 = $post->post_content;</p> <p>// We will search for the src="" in the post content<br /> $regular_expression = '~src="[^"]*"~';<br /> $regular_expression1 = '~<img [^\>]*\ />~';</p> <p>// WE will grab all the images from the post in an array $allpics using preg_match_all<br /> preg_match_all( $regular_expression, $posttext, $allpics );</p> <p>// Count the number of images found.<br /> $NumberOfPics = count($allpics[0]);</p> <p>// This time we replace/remove the images from the content<br /> $only_post_text = preg_replace( $regular_expression1, '' , $posttext1);<br /> /*Only text will be printed*/<br /> echo $only_post_text;</p> <p>// Check to see if we have at least 1 image<br /> if ( $NumberOfPics > 0 )<br /> {<br /> /* Here you can do what ever you want<br /> I used http://code.google.com/p/timthumb/ timthumb script to crop all the images and display in thumbnails.<br /> for that upload the timthumb.php file in your theme folder.<br /> */</p> <p>for ( $i=0; $i < $NumberOfPics ; $i++ )<br /> { $str1=$allpics[0][$i];<br /> $str1=trim($str1);<br /> $len=strlen($str1);<br /> $imgpath=substr_replace(substr($str1,5,$len),"",-1);<br /> ?><br /> <img src='<?php echo get_bloginfo('template_url')."/timthumb.php?src=".$imgpath."&w=300&h=300&zc=1";?>' alt=""/><br /> <?php };</p> <p>};<br /> ?>

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

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

    If you don't understand the difference between WordPress.com and WordPress.org, 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.

  3. That's not a free hosted WordPress.com blog and we can't help you here. If you don't have a username account at WordPress.org click http://wordpress.org/support/ 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 WordPress.org bloggers.

Topic Closed

This topic has been closed to new replies.

About this Topic