I want to mention off the bat that although I am using some Wordpress specific PHP, the answer will not have anything to do with Wordpress. I have just added it to give the full code source.
I've written a "foreach loop" to "echo" ALL of the images i've uploaded in Wordpress' media library.
However I need to be able to select all the images in a certain folder. (i.e /thisfolder).
I just don't know how to say:
"if any of the values in the $imageURL begin with: http://localhost/testsite/wp-content/uploads/thisfolder/" then include them, else do not."
I tried to use substr() to filter it, but I cannot seem to find a way to get it to work. Any thoughts?
CODE:
<?php
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => - 1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image ) {
$images[] = wp_get_attachment_url( $image->ID );
}
for($i = 0; $i < count($images); $i++) {
$imageURL = $images[$i];
echo $imageURL . '<br /><br />';
}
?>
RESULT:
http://localhost/testsite/wp-content/uploads/thisfolder/01.jpg
http://localhost/testsite/wp-content/uploads/thisfolder/02.jpg
http://localhost/testsite/wp-content/uploads/2016/01/03.jpg
http://localhost/testsite/wp-content/uploads/2016/01/04.jpg
I tried adding:
$URLlength = 'http://localhost/testsite/wp-content/uploads/thisfolder';
$akumalURL = substr( $URLlength, 0, strlen($URLlength)) === $URLlength;
if (in_array($akumalURL, $imageURL)) {
echo 'sucess!';
}
else {
echo 'bummer.....';
}