Feb
16
2009

Removing Images from a WordPress Post: Redux


I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post by Chris Shuld which had an amazing little piece of code that solved my problem. I just had to make a few modifications to it.

<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $content);
echo $postOutput;
?>
If you liked this, please share it.

Tags: , , , , , , , , , , , , ,

Short URL: http://bit.ly/aatn4H

Discussion 12 Comments

  1. Jason on April 30, 2009 at 11:01 pm

    THANK YOU!!!! I have never commented on a blog, but this little piece of code kicked ass!

  2. Mat??as on June 8, 2009 at 1:24 pm

    Hi. Your code really helps me! Just a question. It looks like displaying “plain” text. Do you know how can I get the html code in the content like p, a, h3, etc tags?

  3. Ray on July 21, 2009 at 3:49 pm

    Just use the following:

    wplistcategories(‘usedescfor_title=0′);

    print("wp_list_categories('use_desc_for_title=0'); ");

  4. Dyce on February 11, 2010 at 3:44 pm

    Do i put this code in the page or in the functions?

  5. Dyce on February 11, 2010 at 4:05 pm

    I’m trying to use this with an excerpt function, here is what I have:

    function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
     
        $content = get_the_content($more_link_text, $stripteaser, $more_file);    
     
        $content = apply_filters('the_content', $content);
     
        $content = str_replace(']]&gt;', ']]&gt;', $content);
     
        $content = preg_replace('/]+./', '', $content); 
     
       if (strlen($_GET['p']) &gt; 0) {
     
          echo $content;
     
       }
     
       else if ((strlen($content)&gt;$max_char) &amp;&amp; ($espacio = strpos($content, " ", $max_char ))) {
     
            $content = substr($content, 0, $espacio);
     
            $content = $content;
     
            echo strip_tags($content);
     
       }
     
       else {
     
          echo $content;
     
       }
     
    }

    The excerpt never showed an image, but because of the image placement it didn’t show any text either.

    Now it’s only showing the 1st letter of the post.

  6. Jeff on March 1, 2010 at 2:03 am

    where i must add the code? wp_config.php?

    • c.bavota on March 1, 2010 at 1:59 pm

      You can add that code directly to the file where you want to call the content.

  7. Joel Lundgren on March 1, 2010 at 12:08 pm

    Thank you!
    No my search finaly ends.

  8. Kerry on June 21, 2010 at 5:27 pm

    I’m sorry, I send a comment about this subject and then see this page. thanks lot

  9. lesha on July 14, 2010 at 5:11 am

    Oh, your script is pretty useful ,Thank you so much!!:DDD

  10. Tyler on July 16, 2010 at 1:26 am

    Perfect!! Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.