Dynamic Meta Key For Featured Image Caption

Hi,

Is there a way to use dynamic data to bring the featured image’s caption into a Pro layout, in the format shown below? Presumably this may be possible using the custom key, but I cannot find a list of keys to identify the image caption.

{{dc:post:meta key="image-caption"}}

Many thanks,
Christopher

Hi Christopher,

We don’t have a way to do that in Pro at the moment but it is possible by extending Dynamic Content with some custom code in functions.php of a child theme.

add_filter('cs_dynamic_content_post-custom', function( $result, $field, $args ){
  
  global $post;
  
  if (! is_a( $post, 'WP_Post' ) ) {
    return $result;
  }

  if ($field === 'caption') {
    $result = wp_get_attachment_caption( get_post_thumbnail_id( $post ) );
  }

  return $result;

},10, 3);

That code will let you do {{dc:post-custom:caption}} to retrieve the caption of the current thumbnail. I’ll make a note so we can consider possibly making it a new Dynamic Content option but that should work in the meantime.

1 Like

Hi @alexander,

That works perfectly!

Thank you,
Christopher

Hello Christopher,

You are most welcome. If there is anything else we can help you with, please do not hesitate to open another thread.

Cheers.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.