I am currently in the process of making a Wordpress gallery that draws directly from the Media Library and incorporates Isotope and Photoswipe. Using a modified version of this, I have successfully added the attachments to a page template and then restricted the display to categories (with categories for media attachments).
My issue now is further refining the display...
As I am using wp_get_attachment_image
in my function (so I can define the image size), rather than wp_get_attachment_url
, I can't get the href
working. In addition to this, I need to define the full image size within the data-size
of the href
.
I'm also trying to draw the image title and caption, as well as apply the categories of each image as classes to the containing div
.
Here is the code I'm using:
function get_images_from_media_library() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 25,
'category_name' => 'artwork',
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_image( $image->ID, 'full' );
}
return $images;
}
function display_images_from_media_library() {
$imgs = get_images_from_media_library();
$url = wp_get_attachment_url( $image->ID );
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$html = '<section class="isotope-wrap">
<div class="isotope gallery" data-pswp-uid="1">
<div class="isotope-sizer"></div>';
foreach($imgs as $img) {
$html .= '<div class="isotope-item"><figure>
<a href="' . $url . '" data-size="">' . $img . '</a>
<figcaption class="wp-caption-text"><h2>' . $image_title . '</h2>
<p>' . $caption . '</p></figcaption></figure></div>';
}
$html .= '</div>';
return $html;
}
Finally, I'm also trying to build a filter group for the Isotope gallery, using the image categories as the labels. Here is the code I have so far, which doesn't seem to be working since my categories are not counting the images (all categories show a count of 0):
$terms = get_terms('category', array('parent' => 'artwork'));
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<button type='button' class='btn btn-dark' data-hover='" . $term->name . "' data-filter='." . $term->slug . "'>" . $term->name . "</button>\n";
}
}
Any help with this would be greatly appreciated, thanks.
EDIT I added the following code:
function get_image_urls_from_media_library() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'category_name' => 'artwork',
);
$query_images = new WP_Query( $args );
$urls = array();
foreach ( $query_images->posts as $image) {
$urls[]= wp_get_attachment_url( $image->ID );
}
return $urls;
}
and added a nested foreach
, which is drawing a valid href
. Unfortunately, however, it's only drawing the first attachment entry and all images in the gallery are linking to that same first image. The images are also duplicated 5 times on the page.