Display all images from a WordPress post

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!

+4
source share
4 answers
 $attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID')); foreach($attachments as $att_id => $attachment) { $full_img_url = wp_get_attachment_url($attachment->ID); // Your Code here } 

You can also look here: http://www.rlmseo.com/blog/get-images-attached-to-post/

+8
source

Try it! It might work.

 function getImage() { global $more; $more = 1; $link = get_permalink(); $content = get_the_content(); $count = substr_count($content, '<img'); for($i=1;$i<=$count;$i++) { //move $start = 0 inside the loop $start = 0; $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; } $content = substr($content,$imgEnd+1); } $more = 0; } 
+1
source

now easier with the new wordpress function get_attached_media ($ type, $ post)

 $attachments= get_attached_media( 'image', $post->ID ); foreach($attachments as $att_id => $attachment) { $full_img_url = wp_get_attachment_url($attachment->ID); // You can echo it out here } 

note that only files uploaded are included in the message. not files added through the media library.

+1
source
 <?php if ( have_posts() ) while ( have_posts() ): the_post(); $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { echo wp_get_attachment_image( $attachment->ID, false ); } } endwhile; ?> 

Source: http://960development.com/code-snippet/get-all-the-images-attached-with-a-wordpress-post/

0
source

All Articles