Created
May 23, 2014 11:32
-
-
Save yanknudtskov/b5eb6b2bacb1ee4c56c2 to your computer and use it in GitHub Desktop.
Get image tag through an Advanced Custom Field Image Object with parameters.
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 va_string_is_null_or_empty($string) { | |
return (!isset($string) || trim($string) === ''); | |
} | |
function va_get_acf_imagetag_from_image_object( $image_object_fieldname, $image_size = '', $is_option_field = false, $is_sub_field = false ) { | |
$image_size = ''; | |
$image_width = ''; | |
$image_height = ''; | |
$image_object = ''; | |
if(va_string_is_null_or_empty($image_object_fieldname)) | |
{ | |
return _e('Error: field name is not set', 'custom_theme'); | |
} | |
if($is_option_field) | |
{ | |
if($is_sub_field) | |
{ | |
if(!get_sub_field($image_object_fieldname, 'option')) | |
{ | |
return _e('Error: OPTION sub_field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
} | |
$image_object = get_sub_field($image_object_fieldname, 'option'); | |
} | |
else | |
{ | |
if(!get_field($image_object_fieldname, 'option')) | |
{ | |
return _e('Error: OPTION field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
} | |
$image_object = get_field($image_object_fieldname, 'option'); | |
} | |
} | |
else | |
{ | |
if($is_sub_field) | |
{ | |
if(!get_sub_field($image_object_fieldname)) | |
{ | |
return _e('Error: sub_field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
} | |
$image_object = get_sub_field($image_object_fieldname); | |
} | |
else | |
{ | |
if(!get_field($image_object_fieldname)) | |
{ | |
return _e('Error: field named "'. $image_object_fieldname . '" is not set', 'custom_theme'); | |
} | |
$image_object = get_field($image_object_fieldname); | |
} | |
} | |
if( !empty($image_object) ) | |
{ | |
if(!va_string_is_null_or_empty($image_size)) | |
{ | |
$image_size = $image['sizes'][ $image_size ]; | |
$image_width = $image['sizes'][ $image_size . '-width' ]; | |
$image_height = $image['sizes'][ $image_size . '-height' ]; | |
return '<img src="' .$image_size . '" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '" width="' . $image_width . '" height="' . $image_height . '" />'; | |
} | |
else | |
{ | |
return '<img src="' . $image_object['url'] . '" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '">'; | |
} | |
} | |
else | |
{ | |
return _e('Error: image_object is not set', 'custom_theme'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment