Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Exclude images in post from gallery output WordPress

  • SOLVED

Hello!
I am using some code to grab all the images attached to a post and display them in a gallery. This works great, but I just ran into a small issue I overlooked with post images.

When a user adds an image into the body of the post, it also gets added into the gallery. http://cl.ly/DTNW

Is it possible to exclude images used in the post from the gallery output? Here is the code I'm using for the gallery:



<!-- Grab the image attachments -->
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {


echo "<div class='gallery-wrap'><div class='flexslider'><ul class='slides'>";
foreach ($attachments as $attachment) {
echo "<li>";
echo wp_get_attachment_link($attachment->ID, 'large-image', false, false);
echo "</li>";
}
echo "</ul></div></div>";

if(count($attachments) > 1) {
?>



Thanks!
Mike

Answers (4)

2012-01-19

Kailey Lampert answers:

This assumes that the images in the post were inserted via the Insert into Post button and have the 'wp-image-{n}' class name. The regex looks for images with that class name and extracts the ID number. IDs are then passed to the attachments query in the 'exclude' parameter.

I'm not a regex genius, so there may be more efficient ways of doing what I did.


//find images in the content with "wp-image-{n}" in the class name
preg_match_all('/<img[^>]?class=["|\'][^"]*wp-image-([0-9]*)[^"]*["|\'][^>]*>/i', get_the_content(), $result);
//echo '<pre>' . htmlspecialchars( print_r($result, true) ) .'</pre>';
$exclude_imgs = $result[1];

$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'exclude' => $exclude_imgs, // <--
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);


Mike McAlister comments:

This worked perfectly! Thanks, Kailey.

Mike

2012-01-18

John Cotton answers:

There's no way of excluding them from the query - WordPress doesn't store any extra info to say that an image is being used - other than in the post HTML itself.

So the only way to do it would be either:

a/ preg_match the html to pull out a list of image names and then compare that array to the query response
or
b/ do a string match for each image in the gallery loop to see if it appears in the HTML.

If you galleries are small and HTML short, then I suspect the latter is the better in terms of performance. As those ratios/sizes increase, there will come a point where it's better to pull out the list first with a preg_match.

2012-01-18

Ross Wilson answers:

If you wanted to remove all image tags from a post you could do a regex on the post content like so
$string = preg_replace("/<img[^>]+\>/i", "", $string);

If you just wanted to do the images which are in the gallery you might have to come up with a loop to match on the src attribute of the img tag

Edit: Sorry, this may be the opposite of what you wanted

2012-01-19

Fahad Murtaza answers:

Would you like a way of manually providing the IDs of the images and remove them from gallery? I have once fixed this situation.