Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arunbasillal/a62a117248b8cc08d948c171713308ac to your computer and use it in GitHub Desktop.
Save arunbasillal/a62a117248b8cc08d948c171713308ac to your computer and use it in GitHub Desktop.
IAP: Custom tag %pr_acf_author% to use ACF "Author" Field.
/**
* Custom tag %pr_acf_author% for Image Attributes Pro to use ACF Author Field.
*
* @param $image_id (integer) The ID of the image that is being updated.
* @param $parent_post_id (integer) Post to which the image is attached (uploaded) to.
* @param $args (array) An array containing additional arguments.
*
* @return (string) Author of the post / page / product as defined in ACF, if available. Empty string otherwise.
*
* @refer https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/
*/
function iaffpro_get_custom_attribute_tag_pr_acf_author( $image_id, $parent_post_id, $args = array() ) {
if ( $parent_post_id == 0 ) {
return '';
}
// Return if ACF is not installed.
if ( ! function_exists( 'get_field' ) ) {
return '';
}
$author = get_field( 'author', $parent_post_id );
$author_name = '';
if ( $author ) {
foreach ( $author as $post_ids ) {
$author_name = $author_name . get_the_title( $post_ids ) . ' ';
}
}
return trim( $author_name, " ," );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment