Chris Schuld's Blog

personal musings on the composition of software

Written By: Chris Schuld Saturday, November 14th, 2009

A while back I wrote an article on Removing Images from a WordPress Post. Sebastian asked an interesting question; he wanted to remove everything but the images. This is actually pretty straightforward; here is how you do it:

 
.
.
.
<?php
           $beforeEachImage = "<div>";
           $afterEachImage = "</div>";
           preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
           for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
                 echo $beforeEachImage . $matches[1][$i] . $afterEachImage;
           }
?>
.
.
.

Keep in mind this code needs to be in the WordPress Loop and you can control what is around each image using the variables beforeEachImage and afterEachImage above.

Categories: WordPress

2 Responses to " Removing everything BUT Images in a WordPress Post "

  1. Kris says:

    Hi Chris,

    This is amazing! Is there a way to put each image into an array? I want to use your other example with this one to create a custom excerpt.

    thanks.

    Kris

  1. [...] EDIT: make sure you check out the updated version of this solution. Also, view my solution for removing everything but the images. [...]

Leave a Reply

About Chris

Chris Schuld

Thank you for visiting my website. I use this site for a myriad of things: maintaining software I have open sourced, connecting with my readers and friends and documenting all of this little things "I wished I would have written down." You can read more about me or contact me.


thanks for visiting... happy coding

Recent Comments