I have a repeating image field. I'd like to output those images in my theme template as thumbnails with links to their full-size versions. I've seen a couple of posts about this in this forum but the answers didn't work... https://toolset.com/forums/topic/repeating-images-for-slider-in-php-not-shortcodes/
Using luoy's code from the above post, I am able to output the images, but it doesn't return the thumbnail versions. I'm also getting a PHP notice "Notice: Trying to get property of non-object".
Is this not possible with Types? I have Views as well, but I couldn't figure out how to do it with that either.
Could you post the PHP codes you are using, I need test and debug in my localhost, thanks
Here's the relevant code:
$sp_slug = 'site_plan_tab_images';
$sp_html = '';
$site_plan_images = get_post_meta( $the_id, 'wpcf-'.$sp_slug );
$site_plan_images = array_filter( $site_plan_images );
$sp_html = '';
if ( !empty( $site_plan_images ) ) :
foreach( $site_plan_images as $k=>$v ) {
$field = wpcf_fields_get_field_by_slug( $sp_slug );
$params = array( "alt" => "", "title" => "", "size" => "thumbnail", "proportional" => "true", "url" => "true" );
$params['field_value'] = $v;
$thumbnail = types_render_field_single( $field, $params, null, '', $k );
$params['size'] = 'full';
$fullsize = types_render_field_single( $field, $params, null, '', $k );
$sp_html .= '<a href="' . $fullsize . '"><img src="' . $thumbnail . '"></a>';
}
$tabs_content[] = $sp_html;
endif;
The output I'm getting from the above is this:
<a href="<em><u>hidden link</u></em>"><img src="<em><u>hidden link</u></em>"></a>
Along with the php notice I mentioned in the original post.
Thanks in advance!
Please try modify the codes as below:
$sp_slug = 'site_plan_tab_images';
$sp_html = '';
$site_plan_images = get_post_meta( $the_id, 'wpcf-' . $sp_slug );
$site_plan_images = array_filter( $site_plan_images );
$sp_html = '';
if ( !empty( $site_plan_images ) ) :
$field = wpcf_fields_get_field_by_slug( $sp_slug );
foreach( $site_plan_images as $k=>$v ) {
$params = array( "size" => "thumbnail", "proportional" => "false", "url" => "true" );
$params['field_value'] = $v;
$thumbnail = types_render_field_single( $field, $params, null, '', $k );
$params['size'] = 'full';
$fullsize = types_render_field_single( $field, $params, null, '', $k );
$sp_html .= '<a href="' . $fullsize . '"><img src="' . $thumbnail . '"></a>';
}
$tabs_content[] = $sp_html;
endif;
Please replace "site_plan_tab_images" with your custom image field slug, make sure the var $the_id works correct.
That worked! So, moving
$field = wpcf_fields_get_field_by_slug( $sp_slug );
outside of the foreach loop seems to be what fixed it. Can you explain?
Thanks for your help!