Отображение всех изображений из сообщения WordPress

У меня есть этот фрагмент кода, который я нашел в каком-то блоге, который должен отображать все изображения из сообщения WordPress.

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; } 

Что происходит, хотя … он отображает первое и второе изображение правильно, а затем отображает второе изображение вместо 3-го 4-го и т. Д. Он захватывает количество изображений в порядке, но вместо отображения 1-го, 2-го, 3-го, 4-го изображений он отображает 1, 2, 2, 2 …

Может ли кто-нибудь взглянуть на этот фрагмент и, может быть, придумать, почему это происходит? Я знаю, что код довольно неряшлив, но я просто нашел его на каком-то блоге, будучи новичком PHP и всем 🙂

Вся помощь оценили, спасибо заранее!

 $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 } 

Также вы можете посмотреть здесь: http://www.rlmseo.com/blog/get-images-attached-to-post/

его теперь проще с новой функцией wordpress 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 } 

обратите внимание, что это получает только файлы, загруженные в сообщение. а не файлы, добавленные через медиабиблиотеку.

 <?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; ?> 

Источник: http://960development.com/code-snippet/get-all-the-images-attached-with-a-wordpress-post/

Попробуй это ! Это может сработать.

 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; }