Home › Forums › Add-ons › Gallery Field › Gallery field – echo a custom taxonomy as a class in the image container
I have added a custom taxonomy via my functions file and registered it as “image_category”. It allows a category to be assigned to images in the media folder.
I am using the acf gallery field to add images to the my single-galleries.php page.
Q1 – How can I echo the custom taxonomy assigned to the image into the image container?
Q2 – is there a way of linking the custom category selection directly to the gallery field? Right now categories have to be selected/added in the main Media folder.
Here is what my image container looks like:
<div class="wrapper-gallery" itemscope itemtype="http://schema.org/ImageGallery"> <?php $images = get_field('images'); if( $images ): ?> <?php foreach( $images as $image ): ?> <div class="gallery-image id = "imageGallery"> <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> <a href="<?php echo $image['url']; ?>" itemprop="contentUrl" data-size="<?php echo $image['width'] . 'x' . $image['height']; ?>"> <img src="<?php echo $image['sizes']['large']; ?>" itemprop="thumbnail" alt="<?php echo $image['alt']; ?>" /> </a> <figcaption itemprop="caption description"><?php echo $image['caption']; ?></figcaption> </figure> </div> <?php endforeach; ?> <?php endif; ?> </div> <!-- gallery-wrapper -->
Thanks in advance for any help!!
An image is really a post type “attachment” as I’m sure you know. The ID in the $image is thus the attachments ID. So you should be able to fetch the term like:
//will return an array of term objects. $terms = wp_get_object_terms($image['ID'], 'image_gallery');
ACF uses the same image modals as WordPress core so this is really not an ACF thing. You’ve registered your custom taxonomy and now you need to add it to the modal. I don’t have any code for this but I’m sure you can find some by googling 🙂
There’s also plugins out there and this is one of the most tested: https://wordpress.org/plugins/enhanced-media-library/
Thanks! This did work. I had tried something similar but was using “$image->ID”
It took me bit to figure out where to put it. In case anyone else is wondering, you need to put it INSIDE the foreach statement!
<?php $images = get_field('images'); if( $images ): ?> <?php foreach( $images as $image ): $terms = wp_get_object_terms($image['ID'], 'image_category'); ?>
Another WP genius was helping me and had suggested using wp_get_post_terms, which also works.
Thanks so much for your help!
No problem 🙂
The reason I suggested
wp_get_object_terms rather than
wp_get_post_terms is because the second function is really just a wrapper for the first which is meant to be used with regular posts. I’ve experienced some cases where such a wrapper function would not work for custom post types etc. and as thus always try to avoid using them out of context.
Best of luck in your project!
Thanks again, Jonathan. Stellar support here. Wish I could bring you home and get you to help with all of my WP related questions!
See you again on here someday, I’m sure. 🙂
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.