Wordpress : excluding images 'inserted into post' from get_children Wordpress : excluding images 'inserted into post' from get_children wordpress wordpress

Wordpress : excluding images 'inserted into post' from get_children


Just needed to do the same thing. Your original approach is the way I wanted to do it -- simply exclude any images that had been inserted into the post from appearing in the slider. But I didn't want the client to have to do anything special to make it happen. Here's my code.

$args = array( 'post_type' => 'attachment', 'post_mime_type'=>'image','numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args);preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER);/* $matches[1] holds the urls as an array */foreach ( $attachments as $attachment ) {if(in_array($attachment->guid, $matches[1])){ continue;}wp_get_attachment_image( $attachment->ID , 'slider_size'); }

First bit gets all of the images associated with the post. The $preg_match_all gets all of the images in the post body. Then as we loop through the images to display them in the slider the in_array checks the urls of the images that were inserted with the url of the image about to be added to the slider and skips on to the next one if there is a match.

Thanks for you post, got me thinking in the right direction.


I've updated the code, so now I get any image URLs from the post_content and check them against the slideshow images.

$content = $post->post_content;$inlineImages = array();preg_match( '/src="([^"]*)"/i', $content, $inlineImages ) ;$thumbnail = get_post_thumbnail_id($post->ID);$images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail);if ($images) {    echo '<div id="slideshow">';    foreach ( $images as $attachment_id => $attachment ) {        $image = wp_get_attachment_image_src( $attachment_id,array(900,265));         if (!in_array($image[0],$inlineImages)) {            echo '<img src="'.$image[0].'" width="'. $image[1] .'" height="'. $image[2].'">';        }    }    echo '</div>';}


I think the easiest thing to do would be using the Meteor Slideshow plugin to create a slideshow for each page, then insert the shortcode for the proper slideshow in the content area of the proper page. Yes, it means you'll have to edit each page "outside" the page editor, but it also gives you easy, complete control over which photos do and do not appear in each slideshow, and the shortcode is very easy to put in with the page editor.