Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Arno Welzel

    (@awelzel)

    I assume, with “ACF” you mean “Advanced Custom Fields”?

    If this is the case, you should start with the documentation at https://www.advancedcustomfields.com/resources/get_field/.

    The post ID required for get_field() is also passed to the caption filter.

    So it may be something like this if you want to add the custom field content below the existing caption (untested, but the idea should be clear):

    
    function my_lbwps_caption_caption($caption, $id)
    {
        $fieldContent = get_field('my_field_name', $id);
        
        // Combine original caption and field content with a <br>
        return sprintf('%s<br>%s', $caption, $fieldContent);
    }
    
    add_filter('lbwps_caption_caption', 'my_lbwps_caption_caption', 10, 2);

    `

    • This reply was modified 3 years, 7 months ago by Arno Welzel.
    • This reply was modified 3 years, 7 months ago by Arno Welzel.
    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Thanks again for the speedy response.

    Yes – I was referring to Advanced Custom Fields.

    The snippet you provided returns the post ID, while for ACF to work in this context, it requires the attachment ID.

    How can I get the attachment ID?

    Thread Starter heyjoecampbell

    (@heyjoecampbell)

    Looking at the source code, it looks like your plugin knows and uses the attachment IDs:
    https://share.getcloudapp.com/p9uQorYW

    Plugin Author Arno Welzel

    (@awelzel)

    Your screenshot shows a specific gallery output – however my plugin also needs to work with images outside gallery blocks.

    I do also determine the attachement ID in my plugin – but only if the respective option to get the caption from the database is set – otherwise it is not needed to determine that. Also see here: https://github.com/arnowelzel/lightbox-photoswipe/blob/5.0.18/src/LightboxPhotoSwipe/LightboxPhotoSwipe.php#L391-L405

    Also using the attachement ID means to use the image itself. In this case you could just add whatever you want to the caption of the image on your own and don’t need to use a filter for that which combines a post specific content with the image.

    • This reply was modified 3 years, 7 months ago by Arno Welzel.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Add ACF fields to caption’ is closed to new replies.