Support

Account

Home Forums Add-ons Gallery Field Gallery field – echo a custom taxonomy as a class in the image container

Solved

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!!

  • Hi @alynefrancis

    Q1:
    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');
    

    Q2:
    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. 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Gallery field – echo a custom taxonomy as a class in the image container’ is closed to new replies.