Exclude Images in Post from attachment images

I’m using a simple jQuery image slideshow in the single.php, that is calling all the image attachments to my post. But I want to exclude the images posted within the actual post, but keep all the images actually attached to the post.

I’m using this bit of code to grab all the image attachments, but, unfortunately, it’s also grabbing all the images within the post content, which is not necessary:

Read More
<?php
$attachs = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'attachment',
    'post_parent' => get_the_ID(),
    'post_mime_type' => 'image', // get attached images only
    'output' => ARRAY_A
));

if (!empty($attachs)) {
    foreach ($attachs as $att) {
        // get image's source based on size, 
        // can be 'thumbnail', 'medium', 'large', 'full' 
        // or registed post thumbnails sizes
        $src = wp_get_attachment_image_src($att->ID, 'full');
        $src = $src[0];

        // show image
        echo "<div style='margin: 0 10px 10px 0; float: left'><img src='$src' /></div>";
    }
}
?>

Any recommendations?

Related posts

Leave a Reply

1 comment

  1. In WordPress there is unfortunately no difference between attached images and images inserted into a post. A common solution people use on the web is to add a meta_data to the post called for instance “_inserted_image” (true|false) and use hooks to update this value when you insert and image inline in a post. A bit too complicated in my opinion thought.

    A simple workaround would be to create a category specially for your slideshow and put all images intended for the slideshow in this category from the media library at once. Then just add ‘category’ => ‘for_slideshows’ to your condition in your get posts params.

    $attachs = get_posts(array(
                    'numberposts' => -1,
                    'post_type' => 'attachment',
                    'post_parent' => get_the_ID(),
                    'post_mime_type' => 'image', // get attached images only
                    'output' => ARRAY_A,
                    'category' => 'for_slideshows'
                    ));
    

    Hope it helps.