Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arunbasillal/bc78106e0ee06b9cacded3bccaf625e5 to your computer and use it in GitHub Desktop.
Save arunbasillal/bc78106e0ee06b9cacded3bccaf625e5 to your computer and use it in GitHub Desktop.
Custom tag %ds_page_title% for Image Attributes Pro that returns title of Page
/**
* Custom tag %ds_page_title% for Image Attributes Pro that returns title of Page.
*
* @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) Title of the page to which the image is uploaded to. Post title if page cannot be found.
*
* @refer https://imageattributespro.com/codex/iaffpro_get_custom_attribute_tag_tagname/
*/
function iaffpro_get_custom_attribute_tag_ds_page_title( $image_id, $parent_post_id, $args = array() ) {
// Find all posts that use the given image.
$all_posts_with_image = iaffpro_get_all_posts_by_attachment_id_combined( $image_id );
if ( $all_posts_with_image !== false ) {
foreach ( $all_posts_with_image as $post_id ) {
// Check post type and see if it's a WordPress page.
$post_type = get_post_type( $post_id );
if ( $post_type == 'page' ) {
// Return title of the page.
return get_the_title( $post_id );
}
}
}
if ( (int) $parent_post_id === 0 ) {
return '';
}
// Return post title as fallback.
return get_the_title( $parent_post_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment