Grabbing custom field from Wordpress gallery slider

Caroline's Avatar

Caroline

05 Feb, 2018 07:01 PM

Hi there,

I saw the following in the docs:

Get ACF field value for "Posts" slider
Get ALT attribute of image in "Custom" slider

Just wondering how I can grab a custom field from images in the 'Wordpress Gallery' slider, something like?:

`//Creates {{gallery_link_url}} variable that returns custom field
function newrs_add_custom_alt_variable($m, $data, $options) {
    $m->addHelper('gallery_link_url', function() use ($data) {
        // $data object holds all data about slide
        // print_r($data);
        if(isset($data['image']) && isset($data['image']['attachment_id'])) {
            $attachment_id = $data['image']['attachment_id'];
            return get_post_meta( $attachment_id, '_gallery_link_url', true );
         }
         return '';
    } );
}
add_filter('new_rs_slides_renderer_helper','newrs_add_gallery_link_url', 10, 4);`

Thanks!

  1. Support Staff 1 Posted by Dmitry Semenov on 05 Feb, 2018 07:47 PM

    Dmitry Semenov's Avatar

    Hello,

    In "[gallery]" slider, $data object is a default WordPress attachment object, you can get its ID just via:

    $data->ID
    
  2. 2 Posted by Caroline on 06 Feb, 2018 12:46 PM

    Caroline's Avatar

    Thanks Dmitry, but that doesn't work for me.

    This is the slide markup:

    <div class="rsContent">
      <a href="{{gallery_link_url}}">{{image_tag}}</a>
      {{thumbnail}}
      {{html}}
      {{animated_blocks}}
      {{#link_url}}
      <a class="rsLink" href="{{link_url}}">{{title}}</a>
      {{/link_url}}
    </div>
    

    And this is in functions.php:

    //Creates {{gallery_link_url}} variable
    function newrs_add_custom_alt_variable($m, $data, $options) {
        $m->addHelper('gallery_link_url', function() use ($data) {
            $attachment_id = $data->ID;
            if(isset($attachment_id) ) {
                return get_post_meta( $attachment_id, '_gallery_link_url', true );
            } else {
                return '#';
            }
        } );
    }
    add_filter('new_rs_slides_renderer_helper','newrs_add_gallery_link_url', 10, 4);
    

    But {{gallery_link_url}} returns nothing:

    <a href=""><img class="rsImg rsMainSlideImage" ... ></a>

    I tried setting $attachment_id = 784; and {{gallery_link_url}} still returns nothing.

    What am I missing?

  3. Support Staff 3 Posted by Dmitry Semenov on 06 Feb, 2018 01:31 PM

    Dmitry Semenov's Avatar

    Does:

    $m->addHelper('gallery_link_url', function() use ($data) {
            return 'TEST 123';
    });
    

    work?

  4. 4 Posted by Caroline on 06 Feb, 2018 01:42 PM

    Caroline's Avatar

    No, still empty

  5. Support Staff 5 Posted by Dmitry Semenov on 06 Feb, 2018 02:37 PM

    Dmitry Semenov's Avatar

    Oh, you have incorrect name of the function, if in add_filter you have newrs_add_gallery_link_url - function name should be the same (not newrs_add_custom_alt_variable).

  6. 6 Posted by Caroline on 06 Feb, 2018 02:46 PM

    Caroline's Avatar

    Oops!!!

    Thanks so much

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac