Last active
August 29, 2015 14:13
-
-
Save yanknudtskov/99a11820f9dd7f003d97 to your computer and use it in GitHub Desktop.
Get <img> tag from ACF Image Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function yanco_StringIsNullOrEmpty( $string ) { | |
return ( !isset( $string ) || trim( $string ) === ''); | |
} | |
/** | |
* Advanced Custom Fields Get Image Tag From Image Object | |
* Dependencies: functions/function-common.php | |
* Usage: | |
* <code> | |
* $args = array( | |
* 'class_name' => '', // Output with class | |
* 'image_size' => '', // Name of the image Object | |
* 'only_return_url' => true/false, // Output the URL | |
* 'is_option_field' => true/false, // Look for an option field | |
* 'is_sub_field' => true/false, // Look for a sub_field | |
* 'search_post_id' => $search_post_id // Should we look for the field in a specific post | |
* ); | |
* | |
* yanco_get_acf_imagetag_from_image_object('acf_image_object_fieldname', $args); | |
* </code> | |
* | |
* | |
*/ | |
$debug_output_errors = true; // Output debug information? | |
function yanco_get_acf_imagetag_from_image_object( $acf_image_object_fieldname, $args = array() ) | |
{ | |
$image_width = ''; | |
$image_height = ''; | |
$image_object = ''; | |
$image_result = ''; | |
$class_name = ''; | |
$image_size = ''; | |
$only_return_url = false; | |
$is_option_field = false; | |
$is_sub_field = false; | |
$search_post_id = null; | |
if( array_key_exists( 'class_name', $args ) ) | |
$class_name = $args['class_name']; | |
if( array_key_exists( 'image_size', $args ) ) | |
$image_size = $args['image_size']; | |
if( array_key_exists( 'only_return_url', $args ) ) | |
$only_return_url = $args['only_return_url']; | |
if( array_key_exists( 'is_option_field', $args ) ) | |
$is_option_field = $args['is_option_field']; | |
if( array_key_exists( 'is_sub_field', $args ) ) | |
$is_sub_field = $args['is_sub_field']; | |
if( array_key_exists( 'search_post_id', $args ) ) | |
$search_post_id = $args['search_post_id']; | |
if( yanco_StringIsNullOrEmpty( $acf_image_object_fieldname ) ) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: field name is not set', 'custom_theme'); | |
else | |
return; | |
} | |
if($is_option_field) | |
{ | |
if($is_sub_field) | |
{ | |
if(!get_sub_field($acf_image_object_fieldname, 'option')) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: OPTION sub_field named "'. $acf_image_object_fieldname . '" is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_sub_field($acf_image_object_fieldname, 'option'); | |
} | |
else | |
{ | |
if(!get_field($acf_image_object_fieldname, 'option')) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: OPTION field named "'. $acf_image_object_fieldname . '" is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_field($acf_image_object_fieldname, 'option'); | |
} | |
} | |
else | |
{ | |
if($search_post_id != null) | |
{ | |
if($is_sub_field) | |
{ | |
if( !get_sub_field($image_object_fieldname, $search_post_id) ) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: sub_field named "'. $image_object_fieldname . '" in post_id: '.$search_post_id.' is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_sub_field($image_object_fieldname, $search_post_id); | |
} | |
else | |
{ | |
if( !get_field($image_object_fieldname, $search_post_id) ) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: field named "'. $image_object_fieldname . '" in post_id: '.$search_post_id.' is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_field($image_object_fieldname, $search_post_id); | |
} | |
} | |
else | |
{ | |
if($is_sub_field) | |
{ | |
if( !get_sub_field($image_object_fieldname) ) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: sub_field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_sub_field($image_object_fieldname); | |
} | |
else | |
{ | |
if( !get_field($image_object_fieldname) ) | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
else | |
return; | |
} | |
$image_object = get_field($image_object_fieldname); | |
} | |
} | |
} | |
if( !empty( $image_object ) ) | |
{ | |
if( !yanco_StringIsNullOrEmpty( $image_size ) ) | |
{ | |
$image_size = $image_object['sizes'][ $image_size ]; | |
$image_width = $image_object['sizes'][ $image_size . '-width' ]; | |
$image_height = $image_object['sizes'][ $image_size . '-height' ]; | |
if( !$only_return_url ) | |
{ | |
if( !yanco_StringIsNullOrEmpty($class_name) ) | |
{ | |
return '<img src="' .$image_size . '" class="'.$class_name.'" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '" width="' . $image_width . '" height="' . $image_height . '" />'; | |
} | |
else | |
{ | |
return '<img src="' .$image_size . '" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '" width="' . $image_width . '" height="' . $image_height . '" />'; | |
} | |
} | |
else | |
return $image_size; | |
} | |
else | |
{ | |
if( !$only_return_url ) | |
{ | |
if( !yanco_StringIsNullOrEmpty($class_name) ) | |
{ | |
return '<img src="' . $image_object['url'] . '" class="'.$class_name.'" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '">'; | |
} | |
else | |
{ | |
return '<img src="' . $image_object['url'] . '" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '">'; | |
} | |
} | |
else | |
return $image_object['url']; | |
} | |
} | |
else | |
{ | |
if( $debug_output_errors == true ) | |
return _e('Error: image_object is not set', 'custom_theme'); | |
else | |
return; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment