The 2nd code branch below outputs all images found in the directory ($dir).
I'd just like to exclude any images from the output that appear in the $excludeImages array below:
/* collect attached images into an array to test against */
global $wpdb;
$excludeImages = array();
$excludeImagesFiles = $wpdb->query(
"SELECT meta_value
FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file'");
array_push($excludeImages, $excludeImagesFiles);
This is the code as it exists without the exclude filter...
$imgs = array();
if ($dh = opendir($dir))
{
if(!get_option('cb2_underscores')){$myfilter="^";}else{$myfilter="";}
while (($file = readdir($dh)开发者_开发问答) !== false)
{
if (!is_dir($file)&& $file != "." && $file != ".." && preg_match("/^[".$myfilter."_].*\.(bmp|jpeg|gif|png|jpg)$/i", $file))
// and $file not contained in the $excludeImagesFiles array
{
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
if($imgs)
{
sort($imgs);
echo '<div class="images">';
foreach ($imgs as $idx=>$img)
{
$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo $prelink.' src="' . $url . $img . '" alt="' .$img . '"' . $class . ' />'.$postlink;
}
echo '</div>';
}
What code do I need to insert into the while branch to make sure any of the array images don't get added to the array_push()?
Check whether or not $file matches an entry in the exclusion list array. Add a condition like this:
if (!in_array($file, $excludeFiles)) ...
精彩评论