I have this piece of code that I found on some blog that should display all the images from a WordPress post.
function getImage() { global $more; $more = 1; $link = get_permalink(); $content = get_the_content(); $count = substr_count($content, '<img'); $start = 0; for($i=1;$i<=$count;$i++) { $imgBeg = strpos($content, '<img', $start); $post = substr($content, $imgBeg); $imgEnd = strpos($post, '>'); $postOutput = substr($post, 0, $imgEnd+1); $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);; if(stristr($postOutput,'<img')) { echo $postOutput; } $start=$imgEnd+1; } $more = 0; }
What happens though ... it displays the first and second image correctly, then iterates over the second image instead of the 3rd 4th, etc. It captures the number of images in order, but instead of displaying the 1st, 2nd, 3rd, 4th images, it displays 1, 2, 2, 2 ...
Could someone take a look at this fragment and maybe figure out why this is happening? I know that the code is pretty messy, but I just found it on some blog as a PHP newbie and thatβs all :)
All help is appreciated, thanks in advance!
source share