• Resolved Sprutt

    (@sprutt)


    Hi,

    I’m trying to adapt the setup that you can find here so that instead of pulling in the gallery images thumbnails it will show an album.

    I got it pulling in the album correctly but to get Isotope working I need to insert some tags as classes for the wrapping divs, so Isotope knows what to sort. I have added these tags in a custom field for each gallery.

    For the original setup this code was working fine for extracting the tags from the images built-in tags field:

    <?php
    $tags = wp_get_object_terms($image->pid,'ngg_tag');
    $tag_string = '';
    ?>
    <?php foreach ( $tags as $tag ) : ?>
      <?php $tag_string = $tag_string.$tag->slug.' ';
    <?php endforeach; ?>

    But simply switching out

    $tags = wp_get_object_terms($image->pid,'ngg_tag');

    for

    $tags = nggcf_get_gallery_field($gallery->ID, "Tags");

    is throwing “Invalid argument supplied for foreach()…” errors.

    Does anyone know how I can access and extract the values in that custom field?

    Thanks in advance for any help on this!

    https://www.ads-software.com/extend/plugins/nextgen-gallery-custom-fields/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author shauno

    (@shauno)

    Hi Sprutt

    Looking at the WP docs, it seems that wp_get_object_terms() returns an array of the terms (see here).

    The nggcf_get_gallery_field() function will just return the value of the custom field, so it will return a string.
    If you have a comma separated list of terms in that field, you want explode them into an array like this after getting them:
    $tags = explode(',', $tags);

    If you have spaces between your commas in that field, then you will need to trim the whitespace too. So your code snippet will become this:

    <?php
    $tags = nggcf_get_gallery_field($gallery->ID, "Tags");
    $tags = explode(',' $tags);
    $tag_string = '';
    ?>
    <?php foreach ( $tags as $tag ) : ?>
      <?php $tag_string = $tag_string.trim($tag).' ';
    <?php endforeach; ?>

    This is all untested, just going by experience here. Let me know how that goes. If there is still trouble I will replicate your setup and have a proper go at it ??

    Thread Starter Sprutt

    (@sprutt)

    Many, many thanks Shauno!

    That works great, I just had to switch the ‘ID’ for ‘gid’ and it was golden.

    Again, thanks for a great plugin and support, have a nice weekend!

    Plugin Author shauno

    (@shauno)

    Sweet, glad that (mostly) was right then.
    Feel free to leave a rating here if you want ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Loop through comma-separated string in gallery custom field’ is closed to new replies.