Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress : excluding images 'inserted into post' from get_children

I have a page which has a slideshow at the top, and images inserted inline into the content area.

I need to exclude the images that have been inserted into the post from the slideshow.

Currently I am excluding the 'Featured Image', but this limits me to one image that can be inserted into the post.

Here is my existing code:

$thumbnail = get_post_thumbnail_id();
$images = get_children( 'post_type=attachment&post_mime_type=image&order=asc&orderby=menu_order&post_parent='.$post->ID .'&exclude='.$thumbnail);

Previously I have used the description field of the image meta data to exclude images by entering 'exclude'. This isn't as nice for the end user as I'd like it to be.

Any suggestions, plugins or code based!

Update: 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>';
    }

This is an OK solution, although the regex could be improved.

A nicer step would be to add the array of images to a custom field field, which is updated on post / page update or publish.

Any suggestions on how to go about this?

like image 928
addedlovely Avatar asked Jan 01 '12 23:01

addedlovely


2 Answers

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.

like image 71
wunderdojo Avatar answered Oct 27 '22 15:10

wunderdojo


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>';
}
like image 33
addedlovely Avatar answered Oct 27 '22 16:10

addedlovely